我一直在嘗試將一天中最好的部分刪除,因爲它不被mysql支持。如果任何人都可以提供一些指針,這將是非常有用的。mysql INTERSECT的替代
SELECT *, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age
FROM user U, user_utilisation UU
WHERE U.id_user = UU.id_user AND cp >= 1 AND cp <= 3000 AND sexe = 'M' AND UU.id_mailing = 6
GROUP BY U.id_user
HAVING age >= 1 AND age <= 100
ORDER BY nom, prenom
INTERSECT
SELECT *, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age
FROM user U, user_utilisation UU
WHERE U.id_user = UU.id_user AND cp >= 1 AND cp <= 3000 AND sexe = 'M' AND UU.id_mailing = 7
GROUP BY U.id_user
HAVING age >= 1 AND age <= 100
ORDER BY nom, prenom
我試着用JOIN
(一個或多個),但這裏是我現在所擁有的:
SELECT *, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(naissance, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(naissance, '00-%m-%d')) AS age
FROM user U, user_utilisation UU
WHERE U.id_user = UU.id_user AND cp >= 1 AND cp <= 3000 AND sexe = 'M'
AND UU.id_user IN (select id_user from user_utilisation where id_mailing = 6 OR id_mailing = 7)
HAVING age >= 1 AND age <= 100
ORDER BY nom, prenom
而是通過刪除GROUP BY
我看到查詢選擇2條記錄,其中id_mailing = 1
,而GROUP BY
藏匿錯誤的記錄。我敢肯定,這可能會導致問題...
user_utilisation
只有三個字段id_user
,id_mailing
和date
。
對不起,我肯定可能是錯的,但是怎麼能這樣一個路口返回任何結果呢?如果第一個記錄的id_mailing = 6,則選擇一條記錄;如果第二個記錄的id_mailing = 7,則記錄被選中。我認爲不可能是兩個。 – Aioros
你能否向我們提供一個你試圖擺脫查詢的例子? – ESG