简体   繁体   English

MySQL-如果所有值都不同,则显示子表中的空列

[英]MySQL - Display null column from child table if all values are not distinct

I have the following tables, for example: 我有以下表格,例如:

invoices 发票

ID Name
1   A
2   B
3   C
4   D
5   E

transactions 交易

ID  Invoice_ID User_ID
1       1        10
2       1        10
3       1        10
4       2        30
5       3        20
6       3        40
7       2        30
8       2        30
9       4        40
10      3        50

Now I want to make a select that will pull the invoices and the user_id from the related transactions, but of course if I do that I won't get all the ids, since they may be distinct but there will be only one column for that. 现在,我想进行选择,以从相关交易中提取发票和user_id,但是,如果这样做,我将不会获得所有ID,因为它们可能是不同的,但该列只有一列。 What I want to do is that if there are distinct User_ids, I will display a pre-defined text in the column instead of the actual result. 我想做的是,如果有不同的User_id,我将在该列中显示预定义的文本,而不是实际结果。

select invoices.id, invoices.name, transactions.user_id(if there are distinct user_ids -> return null)

from invoices
left join transactions on invoices.id = transactions.invoice_id

and then this would be the result 然后这就是结果

ID Name User_ID
1   A    10
2   B    30
3   C    null
4   D    40
5   E    null

Is this possible? 这可能吗?

You can do the following : 您可以执行以下操作:

select 
   invoices.id, 
   invoices.name, 
   IF (
      (SELECT COUNT(DISTINCT user_id) FROM transactions WHERE transactions.invoice_id = invoices.id) = 1, 
      (SELECT MAX(user_id) FROM transactions WHERE transactions.invoice_id = invoices.id), 
      null
   ) AS user_id
from invoices

Or, alternatively, you can use the GROUP_CONCAT function to output a comma-separated list of users for each invoice. 或者,您也可以使用GROUP_CONCAT函数为每个发票输出用逗号分隔的用户列表。 It is not exactly what you asked, but maybe in fact it will be more useful : 这并不是您所要求的,但实际上可能会更有用:

select 
    invoices.id, 
    invoices.name, 
    GROUP_CONCAT(DISTINCT transactions.user_id SEPARATOR ',') AS user_ids
from invoices
left join transactions on invoices.id = transactions.invoice_id
group by invoices.id

Try somethingh like: 尝试类似的东西:

select i.id, i.name, t.user_id
from invoices i left join
(
   select invoice_ID, User_ID
   from transactions
   group by invoice_ID
   having count(invoice_ID)=1
) t on i.id=t.invoice_id

SQL fiddle SQL小提琴

You could list all the transactions that have multiple user ids, like this: 您可以列出具有多个用户标识的所有事务,如下所示:

select invoices.id, invoices.name, null

from invoices
left join transactions on invoices.id = transactions.invoice_id having count(distinct transactions.user_id) > 1

Also, I think this CASE might suit your needs here: 另外,我认为这种CASE可能适合您的需求:

select invoices.id, invoices.name, 
   case when count(distinct transactions.user_id) > 1 then null else transactions.user_id end     
from invoices
left join transactions on invoices.id = transactions.invoice_id 
group by invoices.id

although, I'm not sure this is syntactically correct 虽然,我不确定这在语法上是否正确

暂无
暂无

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

相关问题 MySQL从列a中为每个不同的列b选择所有不同的值,其中计数不同的a> 1 - MySQL select all distinct values from column a for each distinct column b, where count distinct a >1 MySQL动态显示出现次数的列中的不同值? - MySQL dynamically display distinct values from a column with count of occurrences? MYSQL查询-一种基于同一表中另一列中的DISTINCT值从一列返回所有值的简单方法? - MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table? 从MySQL表列中回显所有值 - Echoing all values from a MySQL table column PHP MySQL:显示给定表中的所有列名以及任何给定行的值 - PHP MySQL: Display all column names from a given table and the values for any single given row MySQL查询从一个表中选择不同的值,并从一个单独的表中选择所有值 - MySQL query to select distinct values from one table and all values from a separate table MySQL:如果另一列在连接表中有两个确定的值,则从一列中选择不同的值 - MySQL: Select distinct values from one column if other column has two determined values in junction table 如果有 2 个相似行,则从 column1 获取所有不同的值,获取 column2 不是 null 的值 - Get all distinct values from column1 in case of 2 similar rows, get the one with column2 not null IS NULL未在MYSQL列中找到所有空值 - IS NULL not finding all null values in column MYSQL 在mysql中:如何显示所有列与一列不同 - In mysql:how to display all column with distinct for one column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM