[英]How to access subquery table in the main query with MySQL?
select m1.id, m1.status, at.view_data, at.view_graph, ta.tag_string
from
access_tbl at, image_campaign_tbl m1
RIGHT JOIN
(select
GROUP_CONCAT(t.name) as tag_string , c.image_campaign_id
from campaign_tags_tbl c,tag_tbl t
where c.tag_id=t.id
$tag_q
group by c.image_campaign_id
) as ta
ON ta.image_campaign_id=m1.id
where
m1.client_id =$client_id
and m1.client_id = at.client_id
$prev_filter
limit $start,$end;
錯誤信息:
在日志中:DBD :: mysql :: db selectall_arrayref失敗:/home/sakthi/rtads/Project/pm/Image/UI.pm第2536行的'where子句'中的未知列't.name'
在Perl Module中,我將相同的$ tag_q值傳遞給$ prev_filter以獲得下一頁中基於TAGS值的過濾器分頁
if ( $prev_filter eq '' ) {
$prev_filter =
$search_clist_q . ' '
. $tag_q . ' '
}
從錯誤消息,我得到了我正在做的錯誤。 由於我正在嘗試訪問主查詢中的子查詢表,因此發生了此錯誤。
所以我想知道如何在子查詢之外訪問tag_string(或)t.name。
首先,我建議您避免對FROM table1, table2,... WHERE table1.column1 = table2.column2 AND ...
使用過時的語法( FROM table1, table2,... WHERE table1.column1 = table2.column2 AND ...
)。
以下查詢似乎返回您要查找的內容:
SELECT IC.id
,IC.status
,A.view_data
,A.view_graph
,TA.tag_string
FROM access_tbl A
INNER JOIN image_campaign_tbl IC ON IC.client_id = A.client_id
AND IC.client_id = $client_id
RIGHT JOIN (SELECT CT.image_campaign_id
,GROUP_CONCAT(T.name) AS [tag_string]
FROM campaign_tags_tbl CT
INNER JOIN tag_tbl T ON T.id = CT.tag_id
GROUP BY CT.image_campaign_id) TA ON TA.image_campaign_id = IC.id
WHERE <Your filters here>
LIMIT $start, $end
希望這會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.