簡體   English   中英

sql查詢中where子句的值是select的父列

[英]Value of where clause in sql query is the parent column of select

我有如下查詢。 如圖所示,它具有一個子查詢。 我想要的是使where子句的值應基於s1.id 目前我將其設置為靜態2因為我where s2.id = s1.id嘗試過where s2.id = s1.id但它不起作用:

SELECT 
s1.id,
s1.code,
(SELECT 
        c.company_name
    FROM
        company c
            INNER JOIN
        (SELECT 
            a.company_id
        FROM
            agent a
        INNER JOIN shop s2 ON a.id = s2.agent_id
        WHERE
            s2.id = 2) AS agent_shop ON agent_shop.company_id = c.id) AS agent_company_name
FROM
     shop s1

有辦法實現嗎?

我相信這可以通過簡單的連接來完成:

select
shop.id,
shop.code,
company.company_name as agent_company_name
from
shop
left join agent on shop.agent_id=agent.id
left join company on agent.company_id=company.id
where
shop.id=2;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM