我有两个不同的表共享一个“is_featured”列。我需要选择所有选中“is_featured”的内容并按“date_display”降序对结果进行排序。
PHOTOS
item_name | date_display | is_featured
photo1 | 01-02-13 | yes
photo2 | 02-12-13 | yes
photo6 | 06-24-12 | no
photo23 | 09-24-12 | no
VIDEOS
item_name | date_display | is_featured
video5 | 01-14-13 | no
video10 | 03-09-13 | no
video30 | 03-21-13 | yes
video17 | 11-14-12 | yes
DESIRED RESULTS - All FEATURED CONTENT Sorted by Date DESC
item_name | date_display | is_featured
video30 | 03-21-13 | yes
photo2 | 02-12-13 | yes
photo1 | 01-02-13 | yes
video17 | 11-14-12 | yes
我可以通过这个 UNION 查询得到我想要的结果:
SELECT *
FROM (SELECT item_name, date_display
FROM photos
WHERE is_featured='yes'
UNION
SELECT item_name, date_display
FROM videos
WHERE is_featured='yes'
) x
ORDER BY date_display DESC
我现在需要的是能够为每个表的 item_names 添加唯一标识符。例如:photos.item_name 和 videos.item_name。
我的查询可能吗?
我最终需要执行以下操作: If (item_name is from Photos table) { do this }