开发者

MySQL multiple variables in LEFT JOIN

开发者 https://www.devze.com 2023-01-18 22:50 出处:网络
I have table dates as following \"dates\"\"one\" \"two\"\"three\" \"four\" date1id1id2id3id4 date2id3id1id4id2

I have table dates as following

"dates"  "one" "two"  "three" "four"
date1    id1    id2    id3    id4
date2    id3    id1    id4    id2

and a second table id2name

"ids"  "names"
id1    name1
id2    name2
id3    name3
id4    name4

I have a mental block trying to write

SELECT * FROM dates WHERE `date`='$date' LEFT JOIN `id2name` ON ...

开发者_StackOverflowWhat clause do I put in ON to get names instead of IDs?


Your model looks denormalized, but this should get you started:

SELECT
  d.`date`,
  i1.names AS name1,
  i2.names AS name2,
  ...
FROM dates d
LEFT JOIN `id2name` i1 ON ( i1.ids = d.one )
LEFT JOIN `id2name` i2 ON ( i2.ids = d.two )
...
WHERE `date`='$date'


As far as i know you can only join on one id at a time so you'll have to do a join on each column that has these id's

0

精彩评论

暂无评论...
验证码 换一张
取 消