简体   繁体   English

使SQL查询不分组结果

[英]Having SQL Query not group results

I have a sql query that joins a few tables by user_id , the problem is is that I would like to see all the data from these tables. 我有一个通过user_id联接几个表的sql查询,问题是我想查看这些表中的所有数据。

At the moment I am querying the user table and the payment table and joining them based on the user_id . 目前,我正在查询用户表和付款表,并根据user_id它们加入。

I seem to only getting one result per user_id, now I know this it what is meant to happen but how (if possible), would I be able to show all the data from the payment table (ie I want to see multiple user ids that show the various payments that user has made). 我似乎每个user_id只能得到一个结果,现在我知道这意味着要发生什么,但是如何(如果可能的话)可以显示付款表中的所有数据(即我想查看多个用户ID,显示用户已支付的各种款项)。

I am currently using a left join, I'm thinking this may be the issue but switching it still isn't showing the results I am looking for. 我目前正在使用左联接,我想这可能是问题所在,但切换它仍未显示我想要的结果。

This i my current join: 这是我目前的加入:

from user u 
left join u_subscription us 
on u.user_id = us.user_id 
left join u_detail ud 
on u.user_id = ud.user_id

Any help would be hugely appreciated. 任何帮助将不胜感激。

Thanks in advance 提前致谢

Nad 纳德

Full sql query: 完整的SQL查询:

select u.user_id as 'prop:User id', u.user_username as 'prop:username', u.user_email as 'Identity',u.user_created as 'Timestamp', us.user_subscription_expires as 'prop:Expires on', us.user_subscription_payment_provider as 'prop:Payment provider', us.user_subscription_modified, ud.user_detail_signup_country_iso3 as 'prop:Country'

from user u
left outer join user_subscription us
on u.user_id = us.user_subscription_user_id
left outer join user_detail ud
on u.user_id = ud.user_detail_user_id

I suggest using another method: 我建议使用另一种方法:

select
   'your selected columns'
from
   user u,
   u_subscription us,
   u_detail ud
where 
   u.user_id = us.user_id and
   u.user_id = ud.user_id

this works just like the join method but for me, this method makes the query cleaner and easier to understand.. hope it helped! 此方法的工作方式类似于join方法,但对我来说,此方法可使查询更整洁且更易于理解。希望对您有所帮助!

Since you are specifically looking for something of details / payments, use THAT table as the basis of the query, then join to the users table as the detail would only exist based on a user... However, you can left-join to the subscription if that is conditional. 由于您是专门查找明细/付款的信息,因此请使用THAT表作为查询的基础,然后再连接到users表,因为明细仅基于用户存在...但是,您可以左联接到订阅(如果有条件的话)。 Something like 就像是

select
      u.*,
      ud.*,
      us.*
   from
      u_detail ud
         join user u
            on ud.user_id = u.user_id
         left join u_subscription us
            on ud.user_id = us.user_id

Since the userID would be the same from user detail to the user table, just use the same for joining to the subscription table. 由于从用户详细信息到用户表的userID相同,因此只需将其用于加入订阅表即可。 I normally would not use "*" for fields, but pick what you want from it. 我通常不会在字段中使用“ *”,而是从中选择所需的内容。

I find no flaws in your query. 我发现您的查询没有缺陷。 It should be working as expected. 它应该按预期工作。

I'm suspecting there is no more than one row per user in the subscription and detail tables. 我怀疑在subscription表和detail表中, 每位用户最多只能有一行。 That's why your left join produces exactly one row per user (if it was an inner join, it would produce at most one row per user). 这就是为什么您的left join联接每位用户仅产生一行(如果是内部联接,则每位用户最多可产生一行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM