无论 user_id 在 table_one 中出现多少次,我只需要选择他们的任何 user_id,例如选择(user_id = 8 和 user_id = 12)中的任何一个,然后从 table_two 获取他们的姓名和年龄,然后将它们添加到大批。 以下是表格:
表一:
id user_id user_loves
1 8 Mango
2 5 Apple
3 1 Oranges
4 12 Toys
5 8 Guns
6 12 Bats
表二:
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
关于php - Mysql select Any one row FROM table_one WHERE 具有相同值的列多次出现,然后将其数据从 table_two 输出到一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43048559/