简体   繁体   中英

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

Doesn't matter how many times a user_id appears in table_one, I just need select any of their user_ids eg select any of (user_id = 8 and user_id = 12) then get their names and age from table_two then add them into an array. Here are the tables:

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

Get any of user_id = 8 AND user_id = 12; then fetch the corresponding names and age from table_two and populate the result into an accessible array like so:

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

So that my final answer is

firstname = David
secondName = Elsa
second_age = 22

you could use a select distinct and left join

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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