我有一个问题。我正在加入两个表,但无法使用MySQL获取正确的数据。我在下面解释我的表。


  db_day:


day_id        day_name

  1            Monday

  2            Tuesday

  3            Wednesday

  4            Thursday

  5            Friday

  6            Saturday

  7            Sunday



  db_images:


id    name    from_day    to_day

 1     Raj       1          3

 2     Rahul     4          7


我在下面解释我的查询。

select sl.id,sl.name,sl.from_day,sl.to_day,d.day_name,d.day_id from db_images as sl left join db_day as d on d.day_id=sl.from_day sl.id desc


在加入表后,在这里我都需要from day and to day和名称,但是在这里我只得到from day。请帮助我解决此问题。

最佳答案

用不同的别名将db_day表连接两次

select sl.id, sl.name, sl.from_day, sl.to_day,
       d1.day_name as from_day, d1.day_id as from_id,
       d2.day_name as to_day, d2.day_id as to_id
from db_images sl
left join db_day d1 on d1.day_id = sl.from_day
left join db_day d2 on d2.day_id = sl.to_day

关于php - 使用MySQL加入两个表后无法获得两天的名字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40017219/

10-12 01:07