Inner SELECT can't use `u.id` which is in outer SELECT - mysql

I have the following MySQL query:
SELECT
COUNT(b.`id`) AS todayOverdue,
DATE_FORMAT(t.`created_time`, "%Y%m%d") AS days
FROM
`Bill` b
LEFT JOIN `Order` o ON b.`order_id` = o.`id`
LEFT JOIN `Trade` t ON o.`trade_id` = t.`id`
LEFT JOIN `User` u ON b.`user_id` = u.`id`
WHERE
b. `deadline` <= "' . $todayTime . '"
AND b. `deadline` >= "' . $todayDate . '"
AND b.`is_paid` = 0
AND (
SELECT
COUNT(b2.`id`)
FROM
`Bill` b2
WHERE
b2.`deadline` <= "' . $todayTime . '"
AND b2.`user_id` = u.`id`
AND b2.`is_paid` = 0
OR (
b2.`deadline` <= b2.`paid_time`
AND b2.`is_paid` = 1
)
) < 2
GROUP BY
days
Why can't the inner SELECT use u.id which is in outer SELECT?

The inner select is completely independent of the outer select. The u table is being joined in the outer select in ways that are unknown to the inner select.
When you have
AND b2.`user_id` = u.`id`
Which row in u is being compared to which row in b2? The server has no way of knowing, so you need to define a table u2 and join it in the inner select.

Related

sub query inside mysql check value from main table gives error of undefine veriable

my query is like below
SELECT
*
FROM`punit_master` `a`
LEFT JOIN
(
SELECT *
FROM
(
SELECT *
FROM _map_contacts as t
LEFT JOIN contact_master as c on c.contact_id = t.contact_id
where t.punit_id =a.punit_id
limit 1
) tm LEFT JOIN contactdetail_master cd ON cd.contactdetail_id = tm.contactdetail_id
) b ON b.agreement_id = a.curr_tenant_agreement_id
it gives me error of a.punit_id is undefine
how can I use mail table value in left join inside sub query
SELECT
*
FROM `punit_master` `a`
LEFT JOIN
(
SELECT *
FROM
(
SELECT *
FROM _map_contacts as t
LEFT JOIN contact_master as c
ON c.contact_id = t.contact_id
) tm
LEFT JOIN contactdetail_master cd
ON cd.contactdetail_id = tm.contactdetail_id
) b
ON b.agreement_id = a.curr_tenant_agreement_id and a.punit_id = b.punit_id
Try this! :)

subquery returns more than 1 row?

subquery returns more than 1 row ? sulution ?
SELECT `t_files`.*, `t_users`.`username`,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users as b
ON b.id = a.user_id
) as upload_by
FROM `t_files`
LEFT JOIN `t_files_permission`
ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users`
ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1'
AND `t_files_permission`.`id_users` = '59'
AND `is_deleted` =0
Add the condition in your sub query like below
SELECT t_files.*, t_users.username,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users AS b ON b.id = a.user_id
WHERE a.id_users` = '59'
) as upload_by
FROM t_files
LEFT JOIN t_files_permission ON t_files_permission.id_files = t_files.file_id
LEFT JOIN t_users ON t_users.id = t_files_permission.id_users
WHERE t_files.company_id = '1' AND t_files_permission.id_users = '59' AND is_deleted =0
try this way
SELECT `t_files`.*, `t_users`.`username`,
(select CONCAT(b.first_name, " ", b.last_name)
from t_files as a
join t_users as b on b.id = a.user_id
where t.files.id=a.id) as upload_by
FROM `t_files` as
LEFT JOIN `t_files_permission` ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users` ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1' AND `t_files_permission`.`id_users` = '59' AND `is_deleted` =0
I think you don't need sub query. You can use join to resolve stuff.

MYSQL Update AND SUM

i am try to do somethink like this
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`From` FROM `Trans`
) B ON C.`UserID` = B.`From`
SET `Bal` = A.`LSC` - B.`LSC`;
and this is not workink :\
when i just do 1 inner join and run the query twice once to A and second to be it work but i want to do it in one query ...
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
SET `Bal` = A.`LSC`; // Work

MS ACCESS- Editing and undoing SQL causes error

I have a query (equivalent of View in ORACLE, SQL Server) that I saved in an Access database. Now I need to add another column to my select list. But whenever I change the SQL I get errors. Let's say I remove one comma and then put it back. I get errors like "Syntax error after From","Syntax error in Join operation". So if I change anything and then undo the change I still get errors. How can I avoid this?
Edit:Before editing
SELECT A_B.ALICI, Q.QAINOM, M.TIP, Q.QATARIX, Q.CEMIBORC, R2.ADI AS SAT_NOV, M.TAMADI, M.MARK, SA.MIQDAR, SA.SAQIYM, SA.CEMI, SA.FAIZ, A_B.BORC_SU, A_B.MEBLEG, A_B.FERQ, K_G.CEMI_ODEN, A_B.MOBTEL, SA.NOTE
FROM ([SELECT Round(Sum([DBKASSA].[MEBLEQ]),3) AS CEMI_ODEN, DBKASSA.KODAL_GT
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]) AND ((DBKASSA.TARIX)=[INTILLDATE]))
GROUP BY DBKASSA.KODAL_GT]. AS K_G RIGHT JOIN ([SELECT AL.KODALAN,AL.MOBTEL, IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC]) AS BORC_SU, IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]) AS MEBLEG, (IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]))-(IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC])) AS FERQ, AL!OBYEKT & (IIf(AL!NUMAY Is Not Null," - " & AL!NUMAY)) AS ALICI
FROM
(
SELECT DBKASSA.KODAL_GT, Sum(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]))
GROUP BY DBKASSA.KODAL_GT
) AS OD_AL
RIGHT JOIN (
(
SELECT DBQAIME.KODALAN, Sum(DBQAIME.CEMIBORC) AS Su_CEMIBORC
FROM DBQAIME
WHERE (((DBQAIME.KODALAN)=[INBUYERID]))
GROUP BY DBQAIME.KODALAN
) AS B_AL
RIGHT JOIN DBALAN AS AL ON B_AL.KODALAN = AL.KODALAN) ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (((AL.KODALAN)=[INBUYERID]))
]. AS A_B INNER JOIN DBQAIME AS Q ON A_B.KODALAN = Q.KODALAN) ON K_G.KODAL_GT = A_B.KODALAN) INNER JOIN (DBMAL AS M INNER JOIN (DBSA AS SA INNER JOIN [SELECT R2.KOD, R2.VID, R2.ADI
FROM DBRAB2 AS R2
WHERE (((R2.VID)=3))]. AS R2 ON SA.KODEMLNO = R2.KOD) ON M.KODMAL = SA.KODMAL) ON Q.QAINOM = SA.QAINOM
WHERE (((Q.QAINOM)=[INSALEINVOICE]))
ORDER BY M.TIP, M.TAMADI;
After editing;
SELECT A_B.ALICI, Q.QAINOM, M.TIP, Q.QATARIX, Q.CEMIBORC, R2.ADI AS SAT_NOV, M.TAMADI, M.MARK, SA.MIQDAR, SA.SAQIYM, SA.CEMI, SA.FAIZ, A_B.BORC_SU, A_B.MEBLEG, A_B.FERQ, K_G.CEMI_ODEN, A_B.MOBTEL, SA.NOTE
FROM ([SELECT Round(Sum([DBKASSA].[MEBLEQ]),3) AS CEMI_ODEN, DBKASSA.KODAL_GT
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]) AND ((DBKASSA.TARIX)=[INTILLDATE]))
GROUP BY DBKASSA.KODAL_GT]. AS K_G RIGHT JOIN ([SELECT AL.KODALAN,AL.MOBTEL, IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC]) AS BORC_SU, IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]) AS MEBLEG, (IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]))-(IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC])) AS FERQ, AL!OBYEKT & (IIf(AL!NUMAY Is Not Null," - " & AL!NUMAY)) AS ALICI
FROM
(
SELECT DBKASSA.KODAL_GT, Sum(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]))
GROUP BY DBKASSA.KODAL_GT
) AS OD_AL
RIGHT JOIN (
(
SELECT DBQAIME.KODALAN, Sum(DBQAIME.CEMIBORC) AS Su_CEMIBORC
FROM DBQAIME
WHERE (((DBQAIME.KODALAN)=[INBUYERID]))
GROUP BY DBQAIME.KODALAN
) AS B_AL
RIGHT JOIN DBALAN AS AL ON B_AL.KODALAN = AL.KODALAN) ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (((AL.KODALAN)=[INBUYERID]))
]. AS A_B INNER JOIN DBQAIME AS Q ON A_B.KODALAN = Q.KODALAN) ON K_G.KODAL_GT = A_B.KODALAN) INNER JOIN (DBMAL AS M INNER JOIN (DBSA AS SA INNER JOIN [SELECT R2.KOD, R2.VID, R2.ADI
FROM DBRAB2 AS R2
WHERE (((R2.VID)=3))]. AS R2 ON SA.KODEMLNO = R2.KOD) ON M.KODMAL = SA.KODMAL) ON Q.QAINOM = SA.QAINOM
WHERE (((Q.QAINOM)=[INSALEINVOICE]))
ORDER BY M.TIP, M.TAMADI;
By the way, it worked in MS Access 2010. Mine is Access 2003
What has happened is that your subquery or derived table is now bracketed like so
[stuff here].
This causes an error when editing.
The easiest thing to do is use notepad or such like, replace []. with () and paste back.
Try:
SELECT A_B.ALICI,
Q.QAINOM,
M.TIP,
Q.QATARIX,
Q.CEMIBORC,
R2.ADI AS SAT_NOV,
M.TAMADI,
M.MARK,
SA.MIQDAR,
SA.SAQIYM,
SA.CEMI,
SA.FAIZ,
A_B.BORC_SU,
A_B.MEBLEG,
A_B.FERQ,
K_G.CEMI_ODEN,
A_B.MOBTEL,
SA.NOTE
FROM ((SELECT Round(SUM([DBKASSA].[MEBLEQ]), 3) AS CEMI_ODEN,
DBKASSA.KODAL_GT
FROM DBKASSA
WHERE ( ( ( DBKASSA.TIP ) = 1 )
AND ( ( DBKASSA.KODAL_GT ) = [INBUYERID] )
AND ( ( DBKASSA.TARIX ) = [INTILLDATE] ) )
GROUP BY DBKASSA.KODAL_GT) AS K_G
RIGHT JOIN ((SELECT AL.KODALAN,
AL.MOBTEL,
Iif([Su_CEMIBORC] IS NULL, 0, [Su_CEMIBORC])
AS
BORC_SU,
Iif([Su_MEBLEQ] IS NULL, 0, [Su_MEBLEQ])
AS
MEBLEG,
( Iif([Su_MEBLEQ] IS NULL, 0, [Su_MEBLEQ]) ) - (
Iif([Su_CEMIBORC] IS NULL, 0, [Su_CEMIBORC]) )
AS
FERQ,
AL ! OBYEKT & ( Iif(AL ! NUMAY IS NOT NULL,
" - " & AL ! NUMAY) ) AS
ALICI
FROM (SELECT DBKASSA.KODAL_GT,
SUM(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE
( ( ( DBKASSA.TIP ) = 1 )
AND ( ( DBKASSA.KODAL_GT ) = [INBUYERID] ) )
GROUP BY DBKASSA.KODAL_GT) AS OD_AL
RIGHT JOIN ( (SELECT DBQAIME.KODALAN,
SUM(DBQAIME.CEMIBORC) AS
Su_CEMIBORC
FROM DBQAIME
WHERE ((
( DBQAIME.KODALAN ) = [INBUYERID] ))
GROUP BY DBQAIME.KODALAN) AS B_AL
RIGHT JOIN DBALAN AS AL
ON B_AL.KODALAN = AL.KODALAN)
ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (( ( AL.KODALAN ) = [INBUYERID] ))) AS A_B
INNER JOIN DBQAIME AS Q
ON A_B.KODALAN = Q.KODALAN)
ON K_G.KODAL_GT = A_B.KODALAN)
INNER JOIN (DBMAL AS M
INNER JOIN (DBSA AS SA
INNER JOIN (SELECT R2.KOD,
R2.VID,
R2.ADI
FROM DBRAB2 AS R2
WHERE (( ( R2.VID ) = 3 ))) AS R2
ON SA.KODEMLNO = R2.KOD)
ON M.KODMAL = SA.KODMAL)
ON Q.QAINOM = SA.QAINOM
WHERE (( ( Q.QAINOM ) = [INSALEINVOICE] ))
ORDER BY M.TIP,
M.TAMADI;

Limits a left join?

Hi I have a query that pulls the results from a search but I want the left joined results to be limited to 1
Here is the left join:
LEFT JOIN #__ezrealty_siteplan AS sp ON sp.listing_id = a.id "
What I have tried
LEFT JOIN (SELECT * FROM jos_ezrealty LEFT JOIN jos_ezrealty_siteplan AS sp ON (sp.listing_id =jos_ezrealty.id ) LIMIT 1) AS sp ON (sp.listing_id=jos_ezrealty.id)
The entire query:
$query="SELECT a.*, cc.name AS category, ee.ezcity AS proploc, dd.name AS statename, bb.name AS countryname,
u.logo_image AS logo_image, u.mid AS mid, u.dealer_name AS dealer_name, u.dealer_company AS dealer_company,
u.dealer_phone AS dealer_phone, u.dealer_mobile AS dealer_mobile, u.published AS dealerpublished, sp.tenant AS tenant, sp.spacenum AS spacenum, sp.sf AS sf, sp.image AS tenantimage,
u.dealer_type AS dealer_type FROM #__ezrealty as a"
. "\n LEFT JOIN #__ezrealty_catg AS cc ON cc.id = a.cid"
. "\n LEFT JOIN #__ezrealty_locality AS ee ON ee.id = a.locid"
. "\n LEFT JOIN #__ezrealty_state AS dd ON dd.id = a.stid"
. "\n LEFT JOIN #__ezrealty_country AS bb ON bb.id = a.cnid"
. "\n LEFT JOIN #__ezrealty_profile AS u ON u.mid = a.owner"
. "\n LEFT JOIN #__ezrealty_siteplan AS sp ON sp.listing_id = a.id "
. "\n WHERE $extrastring AND a.published = '1' $vacant AND cc.access <= $my->gid $wheres "
. $order.' LIMIT '.$pageNav->limitstart.', '.$pageNav->limit;
In SQL Server, you could use CROSS APPLY. In other DBMS, you can use partitioning (windowing function ROW_NUMBER).
For MySQL, one solution is to use a subquery to get LIMIT 1, then join that to the table, e.g.
select a.col1, a.col2, a.col3, b.col1, b.col2
from
(
select a.col1, a.col2, a.col3, (select b.id as b_id from tbl_b b where b.a_id=a.id limit 1)
from tbl_a a
) a
left join tbl_b b on b.id=a.b_id
I thinks this will give you what you want
SELECT DISTINCT sp.id ,* FROM jos_ezrealty LEFT JOIN jos_ezrealty_siteplan AS sp ON sp.listing_id =jos_ezrealty.id
LEFT JOIN ( SELECT *
FROM #__ezrealty_siteplan
GROUP BY listing_id )
AS sp
ON sp.listing_id = a.id