簡體   English   中英

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

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

我有兩個具有不同列數和列名的表。

標簽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     

標簽b:

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

我想在兩個表中都使用%t%,並且像我一樣在兩個表的所有列中使用適當的發布者嗎?

打印:

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

您正在尋找這樣的東西:

    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演示

UNION用於將來自多個SELECT語句的結果合並為一個結果集。 UNION刪除重復的行, UNION ALL返回所有行。 每個SELECT語句的列號必須相等,因此我在每個SELECT指定了所有字段,將不存在的列設置為Null

請注意 ,以上使用LIKE嚴重影響性能。


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM