簡體   English   中英

Mysql選擇具有相同值的FROM table_one WHERE列中的任何一行出現多次,然后從Array中的table_two輸出其數據

[英]Mysql select Any one row FROM table_one WHERE columns having the same value occurs more than once, then output their data from table_two in an Array

不管user_id在table_one中出現多少次,我只需要選擇它們的任何user_id,例如,選擇任何一個(user_id = 8和user_id = 12),然后從table_two獲取它們的名稱和年齡,然后將它們添加到數組中即可。 表格如下:

table_one:

id   user_id   user_loves
1      8       Mango
2      5       Apple
3      1       Oranges
4      12      Toys
5      8       Guns
6      12      Bats

table_two:

id   user_id    name      Age
 1      8       David     19
 2      5       Michael   24
 3      12      Elsa     22
 4      4       Greg      26

獲取任何user_id = 8 AND user_id = 12; 然后從table_two獲取相應的名稱和年齡,然后將結果填充到一個可訪問的數組中,如下所示:

$result = array();
firstname = $result[0][name];
second_age - $result[1][age];

所以我的最終答案是

firstname = David
secondName = Elsa
second_age = 22

您可以使用選擇的獨特和左聯接

select distinct t1.user_id, t2.name,  t2.age 
from table_one t1
left join table_two t2 on t1.user_id = t2-user_id 

暫無
暫無

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

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