1
我試圖創建一個返回基於以下結果的查詢過濾結果:MYSQL子查詢與多表查詢
的腳本對我的作品,但現在我需要執行額外的步驟。我需要弄清楚如何過濾掉'user_requests'表中尚未確認的任何'activity.dest_id'的結果,並將其僅應用於'IN'中的最後一個值。在這個例子中,值爲'Bill',所有其他值(如'Fashion,Art,Architecture')都不需要針對user_requests表進行檢查,因爲這些值不是人物。謝謝您的幫助。
SELECT a.activity_type, a.dest_id, o.opinion_id, o.fromUser, o.image_data, o.votedYes, o.votedNo, o.created_at, o.expires_at FROM activity a, opinions o WHERE a.opinion_id = o.opinion_id AND NOT EXISTS ( SELECT opinion_id, fromUser FROM user_activity u WHERE u.opinion_id = a.opinion_id ) AND a.dest_id IN ( "Fashion", "Art", "Architecture", "Bill" )
user_activity table +------------+-----------+ | opinion_id | fromuser ------------+-----------+ | 9 | Jim | 10 | Sam +------------+-----------+
user_requests table +------------+-----------+---------+ | fromUser | toUser | type | status ------------+-----------+----------+ | Ana | Bill | C | 1 | Kate | Bill | P | 0 +------------+-----------+---------+
activity table +------------+-------+ | fromUser | dest_id ------------+--------+ | Ana | Fashion | Ana | Art | Kate | Bill | Tom | Fashion | Bill | Fashion | Kim | Art | Kim | Art | Ana | Fashion | Kim | Ana +------------+-------+
opinion table +------------+-----------+---------+------------+-----------+---------+--------------+ | opinion_id | fromUser | image_data | votedYes | votedNo | created_at | expires_at +------------+-----------+---------+------------+-----------+---------+--------------+ | 1 | Ana | non | 1 | 3 | 2014-12-17 | 2014-12-18 | 2 | Ana | non | 1 | 3 | 2014-12-17 | 2014-12-18 | 3 | Ana | non | 1 | 3 | 2014-12-17 | 2014-12-18 | 4 | Ana | non | 1 | 3 | 2014-12-17 | 2014-12-18 | 5 | Ana | non | 1 | 3 | 2014-12-17 | 2014-12-18 +------------+-----------+---------+------------+-----------+---------+-------------+