一个查询中多个连接 MySQL? ?

我有以下疑问:

SELECT
dashboard_data.headline,
dashboard_data.message,
dashboard_messages.image_id
FROM dashboard_data
INNER JOIN dashboard_messages
ON dashboard_message_id = dashboard_messages.id

所以我使用 INNER JOIN并抓取 image_id。现在,我想把 image _ id 从 images 表转换成 images.filename

如何将其添加到查询中?

451097 次浏览

您可以像这样简单地添加另一个连接:

SELECT dashboard_data.headline, dashboard_data.message, dashboard_messages.image_id, images.filename
FROM dashboard_data
INNER JOIN dashboard_messages
ON dashboard_message_id = dashboard_messages.id
INNER JOIN images
ON dashboard_messages.image_id = images.image_id

但是请注意,因为它是 INNER JOIN,如果您有一个没有图像的消息,整行将被跳过。如果有这种可能,您可能需要执行 LEFT OUTER JOIN,它将返回所有指示板消息,并且只有在存在一个 image _ filename 时才返回一个 image _ filename (否则将得到 null)

SELECT dashboard_data.headline, dashboard_data.message, dashboard_messages.image_id, images.filename
FROM dashboard_data
INNER JOIN dashboard_messages
ON dashboard_message_id = dashboard_messages.id
LEFT OUTER JOIN images
ON dashboard_messages.image_id = images.image_id

只需添加另一个联接:

SELECT dashboard_data.headline,
dashboard_data.message,
dashboard_messages.image_id,
images.filename
FROM dashboard_data
INNER JOIN dashboard_messages
ON dashboard_message_id = dashboard_messages.id
INNER JOIN images
ON dashboard_messages.image_id = images.image_id

我分享了在一个 SQL 查询中使用两个 LEFT JOINS 的经验。

我有三张桌子:

表1) PatientID 和 PatientName 列组成

表2)预约包括各列的预约 ID、预约日期时间、患者 ID、医生 ID

表3) Doctor 由 DoctorID、 DoctorName 列组成


质疑:

SELECT Patient.patientname, AppointmentDateTime, Doctor.doctorname


FROM Appointment


LEFT JOIN Doctor ON Appointment.doctorid = Doctor.doctorId  //have doctorId column common


LEFT JOIN Patient ON Appointment.PatientId = Patient.PatientId      //have patientid column common


WHERE Doctor.Doctorname LIKE 'varun%' // setting doctor name by using LIKE


AND Appointment.AppointmentDateTime BETWEEN '1/16/2001' AND '9/9/2014' //comparison b/w dates


ORDER BY AppointmentDateTime ASC;  // getting data as ascending order

我写了获得 日期格式如“ mm/dd/广州欢聚时代”的解决方案(在我的名字“ VARUN TEJ REDDY”下)

SQL 中的多连接是通过逐步创建一个接一个的派生表来实现的。 请看下面这个链接来解释这个过程:

Https://www.interfacett.com/blogs/multiple-joins-work-just-like-single-joins/