0

我需要选择尚未查看已完成预订的用户的电子邮件。我当然希望每次预订不超过一封尚未审核的电子邮件,这就是问题所在。我已经尝试选择 DISTINCT 出价(b.bid 和 r.booking_id 是要查找的数字)以及 GROUP BY 解决方案,但它们都没有工作。

虽然他们仍然可能是解决方案,但我无法让它发挥作用。例如,在我的数据库中仅使用两条评论进行测试,其中 bid 和 booking_id 不匹配,我得到同一预订的两行:

username bid booking_id
mail@mail.mail 9 8
mail@mail.mail 9 45

这是我上次运行以解决问题的查询,但正如您在上面看到的结果是相同的。我在哪里错了?先感谢您。

SELECT u.username, b.bid, r.booking_id
FROM users u, reviews r, bookings b
WHERE u.id = b.id_booker
AND b.status =  'FINISHED'
AND b.bid NOT IN (SELECT booking_id FROM reviews WHERE 1)

编辑:三个表的结构 - 只有相关的列名

Users TABLE 
id           // - auto increment value
email        // The emails to which I will send reminders if no review has been posted

Reviews TABLE
rid          // - auto increment value
booking_id   // ID of the booking to which the review refers (bookings.bid)
id_author    // ID of the user who wrote the review (users.id)

Bookings TABLE
bid          // ID of the booking (reviews.booking_id) - auto increment value
id_booker    // ID of the user who made the booking (users.id)
status       // Only looking for finished bookings in this query
4

1 回答 1

0

You could first select all the ids from Bookings.id_booker. Then substract the id's from the user who have allready submited a review.

SELECT id_booker
FROM Bookings
WHERE id_booker NOT IN ( 
    SELECT id_author
    FROM Reviews
)

That would get you the id's from all the users who have not submitted a review.

Last step would be to get the email adressess of these users.

SELECT DISTINCT email
FROM Bookings
INNER JOIN Users ON Users.id = Bookings.id_booker
WHERE id_booker NOT IN ( 
    SELECT id_author
    FROM Reviews
)

I would prefer to only use sub-queries since you only need data from the Users table. But MySQL has serious performance issues on sub-queries so this could be a good solution.

于 2013-04-09T13:06:47.513 回答