a表有多个字段,a_uid/b_uid/c_uid都对应user表的id.
怎么取出这些id对应的userName比较合理呢?
我想到的有两种,第一种是一次性取出user表的id,username然后在程序进行数组比较;
第二种比较呆,在程序多次查数据库取;
不知道大家是怎么设计这种结构的,有什么好的建议?
数据分析师2017-10-01 00:51:46
Looking for a sql many-to-one solution? -PHP Chinese website Q&A-Looking for a SQL many-to-one solution? -PHP Chinese website Q&A
Let’s take a look and learn.
阿神2017-03-16 14:25:22
join3次就行了
SELECT a.*,ua.username AS ua_name,ub.username AS ub_name,uc.username AS uc_name FROM a LEFT JOIN user AS ua ON ua.id=a.a_uid LEFT JOIN user AS ub ON ub.id=a.b_uid LEFT JOIN user AS uc ON uc.id=a.c_uid