Tôi biết các truy vấn kết hợp phải có cùng số cột. Tôi đang cố gắng lấy kết quả từ bảng comments
và kết quả từ bảng strings
, có nhiều kết nối. Làm thế nào để tôi điều này một cách chính xác? Tôi chưa thử nghiệm vì tôi biết tôi sẽ gặp lỗi với số cột khác nhau. Dưới đây là hai truy vấn mà tôi đang cố gắng kết hợp.mysql union số cột khác nhau
truy vấn 1 (chuỗi)
SELECT sub.actionid as usersub,
ftable.`last-time` as lastvisited, updatetable.recent as mostrecent, parent.* FROM `strings` as parent
LEFT JOIN subscribe sub on sub.actionid=parent.id AND sub.userid=:userid
JOIN followers ftable on ((ftable.sid=parent.sid AND ftable.page='1') OR
(ftable.sid=parent.sid AND ftable.position=parent.position AND ftable.page='0')
OR (ftable.profile=parent.starter AND parent.guideline='1')) AND ftable.userid=:userid
JOIN `update-recent` updatetable on
((updatetable.sid=parent.sid AND updatetable.position=parent.position AND updatetable.pageid='0')
OR (updatetable.profile=parent.starter) OR (updatetable.pageid=parent.pageid AND parent.pageid!=0))
WHERE ftable.userid=:userid AND parent.deleted='0' GROUP BY parent.id
truy vấn 2 (bình luận)
SELECT * FROM comments WHERE viewed='0' AND (`starter-id`=:userid OR respondid=:userid)
Tôi muốn đặt hàng các kết quả theo cột timestamp posted
(gần đây nhất) ORDER BY posted DESC
Làm cách nào để kết hợp các truy vấn này?
Không có chức năng tổng hợp trong truy vấn của bạn, vậy điểm của GROUP BY là gì? – Strawberry
có thể trùng lặp của [Liên kết hai bảng với số cột khác nhau] (http://stackoverflow.com/questions/2309943/unioning-two-tables-with-different-number-of-columns) – hakre