[英]Join SQL tables by id field
我在mysql數據庫上有以下sql查詢,該查詢從wp_client返回所有結果,其中form_id為46且date_created在最近7天內...
SELECT *
FROM
wp_client WHERE form_id = '46'
and
cast(date_created as date) >= current_date - interval '7' day
我還有另一個表wp_client_detail,該表存儲了更多我想包含在結果中的信息。 wp_client_detail中的字段client_id與wp_client中的字段ID匹配。
我假設我需要使用JOIN命令,但無法解決問題,我已經嘗試過...
INNER JOIN
wp_client_detail
ON
wp_client.id=wp_CLIENT_detail.lead_id;
但這不起作用,有人可以幫忙嗎?
語法沒有錯,只需確保按正確的順序進行操作即可:
SELECT *
FROM
wp_client
INNER JOIN wp_client_detail ON
wp_client.id=wp_CLIENT_detail.lead_id
WHERE form_id = '46'
and cast(date_created as date) >= current_date - interval '7' day;
如果該語法不起作用,那么我建議您的數據有問題。
也可以使用IN
渲染
select
*
from
wp_client c
where
form_id = '46'
and cast(date_created as date) >= current_date - interval '7' day
id in (select lead_id from wp_CLIENT_detail)
或EXISTS
:
select
*
from
wp_client c
where
form_id = '46'
and cast(date_created as date) >= current_date - interval '7' day
id exists (select 1 from wp_CLIENT_detail d where c.id = d.lead_id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.