[英]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.