MYSQL 限制左连接查询
MYSQL Limiting left join query
我有一个查询,它使用一些左连接来取回数据。困境是我想 LIMIT
和 ORDER BY
左连接之一的结果,从而限制带回的总行数。
我尝试订购的 table 限额是 table4
我的查询:
SELECT SQL_CALC_FOUND_ROWS e.meta_1, a.requestid, b.clientid, c.job_description, d.forename
FROM quotations_request AS a
LEFT JOIN table1 AS b ON (a.assigned_to = b.userid)
LEFT JOIN table2 AS c ON (a.job_costing = c.jobid)
LEFT JOIN table3 AS d ON (a.clientid = d.clientid)
LEFT JOIN table4 AS e ON (e.quotationid = a.requestid)
WHERE a.archived = '0' AND (e.meta_1 = LCASE('requested') )
我试过的:
SELECT SQL_CALC_FOUND_ROWS statuss.meta_1, a.requestid, b.clientid, c.job_description, d.forename,
FROM quotations_request AS a
LEFT JOIN table1 AS b ON (a.assigned_to = b.userid)
LEFT JOIN table2 AS c ON (a.job_costing = c.jobid)
LEFT JOIN table3 AS d ON (a.clientid = d.clientid)
LEFT JOIN (Select meta_1 from table4 where quotationid = a.requestid ORDER BY uploaded_date DESC LIMIT 0, 1)
as statuss ON (statuss.quotationid = a.requestid)
WHERE a.archived = '0' AND (statuss.meta_1 = LCASE('requested') )
结果:
在尝试上面的方法后我得到一个错误提示 a.requestid
is unknown in the where clause (for the last left join)
试试这个:
SELECT SQL_CALC_FOUND_ROWS statuss.meta_1, a.requestid, b.clientid, c.job_description, d.forename
FROM quotations_request AS a
LEFT JOIN table1 AS b ON (a.assigned_to = b.userid)
LEFT JOIN table2 AS c ON (a.job_costing = c.jobid)
LEFT JOIN table3 AS d ON (a.clientid = d.clientid)
LEFT JOIN (
select *
from (select
t.*,
@rn := if(quotationid = @qid, @rn + 1,
if(@qid := quotationid, 1, 1)
) rn
from (
select *
from table4
order by quotationid, uploaded_date desc) t cross join (
select @rn := 0, @qid := -1
) t2
) t where rn = 1
)
as statuss ON (statuss.quotationid = a.requestid)
WHERE a.archived = '0' AND (statuss.meta_1 = LCASE('requested') )
用户变量@rn 和@qid 用于从最新的uploaded_date 表4 中查找前1 行,然后将其与其他表
连接
我有一个查询,它使用一些左连接来取回数据。困境是我想 LIMIT
和 ORDER BY
左连接之一的结果,从而限制带回的总行数。
我尝试订购的 table 限额是 table4
我的查询:
SELECT SQL_CALC_FOUND_ROWS e.meta_1, a.requestid, b.clientid, c.job_description, d.forename
FROM quotations_request AS a
LEFT JOIN table1 AS b ON (a.assigned_to = b.userid)
LEFT JOIN table2 AS c ON (a.job_costing = c.jobid)
LEFT JOIN table3 AS d ON (a.clientid = d.clientid)
LEFT JOIN table4 AS e ON (e.quotationid = a.requestid)
WHERE a.archived = '0' AND (e.meta_1 = LCASE('requested') )
我试过的:
SELECT SQL_CALC_FOUND_ROWS statuss.meta_1, a.requestid, b.clientid, c.job_description, d.forename,
FROM quotations_request AS a
LEFT JOIN table1 AS b ON (a.assigned_to = b.userid)
LEFT JOIN table2 AS c ON (a.job_costing = c.jobid)
LEFT JOIN table3 AS d ON (a.clientid = d.clientid)
LEFT JOIN (Select meta_1 from table4 where quotationid = a.requestid ORDER BY uploaded_date DESC LIMIT 0, 1)
as statuss ON (statuss.quotationid = a.requestid)
WHERE a.archived = '0' AND (statuss.meta_1 = LCASE('requested') )
结果:
在尝试上面的方法后我得到一个错误提示 a.requestid
is unknown in the where clause (for the last left join)
试试这个:
SELECT SQL_CALC_FOUND_ROWS statuss.meta_1, a.requestid, b.clientid, c.job_description, d.forename
FROM quotations_request AS a
LEFT JOIN table1 AS b ON (a.assigned_to = b.userid)
LEFT JOIN table2 AS c ON (a.job_costing = c.jobid)
LEFT JOIN table3 AS d ON (a.clientid = d.clientid)
LEFT JOIN (
select *
from (select
t.*,
@rn := if(quotationid = @qid, @rn + 1,
if(@qid := quotationid, 1, 1)
) rn
from (
select *
from table4
order by quotationid, uploaded_date desc) t cross join (
select @rn := 0, @qid := -1
) t2
) t where rn = 1
)
as statuss ON (statuss.quotationid = a.requestid)
WHERE a.archived = '0' AND (statuss.meta_1 = LCASE('requested') )
用户变量@rn 和@qid 用于从最新的uploaded_date 表4 中查找前1 行,然后将其与其他表
连接