[英]Mysql query join tables is unable to use where condition
我正在開發一個 Wordpress 網站,我需要使用 where 條件獲取連接表值。現在我可以在不使用 where 的情況下獲取值,但它顯示多個值,我沒有獲得特定 id 的對應值。 如果我在出現錯誤的地方使用。 我在下面給出了我的代碼。
$result = $wpdb->get_results("SELECT
wp_candiform.numFormCand,
wp_candiform.questionCand,
wp_candiform.civilCand,
wp_diplom.annee,
wp_diplom.titre,
wp_diplom.nive,
wp_formation.CandFDQ,
wp_formation.titreCandFDQ,
wp_formation.mention,
wp_confirmation.etatand,
wp_confirmation.comment,
wp_confirmation.etat1Cand
FROM wp_candiform
INNER JOIN wp_diplom ON wp_candiform.user_id = wp_diplom .user_id
JOIN wp_formation ON wp_candiform.user_id = wp_formation .user_id
JOIN wp_confirmation ON wp_candiform.user_id = wp_confirmation .user_id
WHERE user_id='$user_id'"
);
由於您正在連接表,因此您需要提及在 Where 條件中比較哪個表的用戶 ID。
使用 where 條件時,需要在 Where 條件中添加表別名。
參考下面的代碼。
$result = $wpdb->get_results("SELECT
wp_candiform.numFormCand,
wp_candiform.questionCand,
wp_candiform.civilCand,
wp_diplom.annee,
wp_diplom.titre,
wp_diplom.nive,
wp_formation.CandFDQ,
wp_formation.titreCandFDQ,
wp_formation.mention,
wp_confirmation.etatand,
wp_confirmation.comment,
wp_confirmation.etat1Cand
FROM wp_candiform
INNER JOIN wp_diplom ON wp_candiform.user_id = wp_diplom .user_id
JOIN wp_formation ON wp_candiform.user_id = wp_formation .user_id
JOIN wp_confirmation ON wp_candiform.user_id = wp_confirmation .user_id
WHERE wp_candiform.user_id='$user_id'"
);
您的代碼中一切都很好,只是查詢中的一個小錯誤。 在末尾
WHERE user_id='$user_id'
到
WHERE wp_candiform.user_id='$user_id'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.