多维mysql选择?

时间:2015-01-29 13:12:05

标签: mysql join multidimensional-array

table: car_profile
carid, carname, caryear, cartype

table: user_profile
userid, username, useremail

table: user_car
id, carid, userid, status
FC: carid, userid

一个carid可以在表userid中有多个user_car(一个车可以由多人使用)

鉴于carid,我想选择caryearcartype[userid, username, useremail]。括号内的一个本身应该是一个数组,因为多个userid是可能的。我不确定这是否是可以的?

1 个答案:

答案 0 :(得分:0)

SELECT可以返回单个值,一行值或一个行表。没有更高维度的选择。下面,多个useridusernameuseremail将显示在不同的行上。

SELECT cp.caryear, cp.cartype, up.userid, up.username, up.useremail
FROM car_profile as cp
LEFT JOIN user_car as uc
ON uc.carid = cp.carid 
LEFT JOIN user_profile as up
ON uc.userid = up.userid
WHERE cp.carid = carid
ORDER BY cp.caryear, cp.cartype