简体   繁体   English

具有相同ID但行名称不同的多行

[英]Multiple rows with same id but different row names

I have two tables with different number of columns and column name. 我有两个具有不同列数和列名的表。

tab a: 标签a:

ID   nome       cognome    messaggio  testo      orario     ip
---  ---------  ---------  ---------  ---------  ---------  ---------
1    a          a          a          a          a          127  
2    b          b          b          b          b          111
3    a          a          tt         qqq        h          127     

tab b: 标签b:

id   nome       cognome    email
---  ---------  ---------  ---------
1    t          t          t

I would like to use %t% within both the tables and proper publisher all columns of both tables as I do? 我想在两个表中都使用%t%,并且像我一样在两个表的所有列中使用适当的发布者吗?

print: 打印:

ID   nome       cognome    messaggio  testo      orario     ip         email
---  ---------  ---------  ---------  ---------  ---------  ---------  ---------
1    t          t          null       null       null       null       t
3    a          a          tt         qqq        h          127        null

You are in search of something like this: 您正在寻找这样的东西:

    SELECT ID as id, nome, cognome, messaggio, testo, orario, ip, NULL AS email 
      FROM `tabA` 
     WHERE nome LIKE '%t%' OR cognome LIKE '%t%'  OR messaggio LIKE '%t%'  OR testo LIKE '%t%'

 UNION ALL

    SELECT id as id, nome, cognome, NULL as messaggio, NULL AS testo, NULL as orario, NULL AS ip, email 
      FROM `tabB` 
     WHERE nome LIKE '%t%' OR cognome LIKE '%t%'  OR email LIKE '%t%'

sqlFiddle demo sqlFiddle演示

UNION is used to combine the result from multiple SELECT statements into a single result set. UNION用于将来自多个SELECT语句的结果合并为一个结果集。 UNION remove duplicate rows, UNION ALL returns all rows. UNION删除重复的行, UNION ALL返回所有行。 The column number of each SELECT statement must be equal, so I specified all fields in each SELECT , setting Null for non-existing columns. 每个SELECT语句的列号必须相等,因此我在每个SELECT指定了所有字段,将不存在的列设置为Null

Please note that above use of LIKE seriously affects performance. 请注意 ,以上使用LIKE严重影响性能。


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

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