I have the following query:
select a.clei, a.partNumber,
(
SELECT count(*) FROM
( SELECT * from search_upload_data s where a.clei is not null AND a.clei = s.clei
UNION
SELECT * from search_upload_data s where a.partNumber is not null AND a.partNumber = s.partNumber
) as t1
) as total
from api_analytics_data a
It is throwing the error:
Error Code: 1054. Unknown column 'a.clei' in 'where clause'
I guess MySQL won't allow a join reference in the inner SQL, but I'm not sure how to get around this.
I need the count from search_upload_data, See SQL Fiddle here: http://sqlfiddle.com/#!9/172ac/2
<=========================================================================>
ADDITIONAL NOTE
I need to figure out how to do this with a UNION. The OR conditions in everyone's answers work on a small scale, but bog down on the actual DB with a lot of data.
Here is the explain (for Rajat's answer):
TRY THIS: Simple join with OR and GROUP BY
SELECT a.clei,
a.partNumber,
COUNT(1) tot
FROM api_analytics_data a
LEFT JOIN search_upload_data s ON (a.clei = s.clei OR a.partNumber = s.partNumber)
AND (a.clei is not null OR a.partNumber is not null)
GROUP BY a.clei, a.partNumber
OUTPUT: http://sqlfiddle.com/#!9/94e556/1
try this
select a.clei, a.partNumber,
( SELECT COUNT(1) As Total
from search_upload_data s
where ( a.clei is not null AND a.clei = s.clei )
OR (a.partNumber is not null AND a.partNumber = s.PartNumber)
) as t1
from api_analytics_data a
This corresponds with your desired output anyway. Just using a regular OR to select either clei or partnumber.
select a.clei, a.partNumber,
(
SELECT count(*) FROM search_upload_data s WHERE
(a.clei is not null AND a.clei = s.clei) OR
(a.partNumber is not null AND a.partNumber = s.partNumber)
) as Total
from api_analytics_data a
select a.clei, a.partNumber,
(
SELECT count(s.id) FROM search_upload_data s where (a.clei is not null AND a.clei = s.clei) OR (a.partNumber is not null AND a.partNumber = s.partNumber)
) as Total
from api_analytics_data a
try now
Related
I want to use data of view in WHERE clause. But getting an error:
create view post_with_answers AS
SELECT DISTINCT postid
FROM (SELECT postid FROM `qa_posts` WHERE `type` = 'Q') AS q1
INNER JOIN (SELECT parentid FROM `qa_posts` WHERE `type` = 'A') AS q2 ON q1.postid = q2.parentid
select count(*)
from qa_posts
where parentid not in post_with_answers
On the last row I am getting this error:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'post_with_answers' at line 3
How to fix that?
Just like you would use a table:
select count(*)
from qa_posts
where parentid not in (select pwa.postid from post_with_answers pwa);
I would caution you from using not in with a subquery. No rows are returned if even one value from the subquery is NULL. For this reason, I recommend NOT EXISTS:
select count(*)
from qa_posts p
where not exists (select 1
from post_with_answers pwa
where p.parentid = pwa.postid
);
In addition, your view definition is a bit over complicated. You don't need subqueries:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq JOIN
qa_posts pa
ON pq.postid = pa.parentid
WHERE pq.type = 'Q' AND pa.type = 'A';
Then, the DISTINCT just adds overhead, so EXISTS is better:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq
WHERE EXISTS (SELECT 1
FROM qa_posts pa
WHERE pq.postid = pa.parentid AND
pa.type = 'A'
)
WHERE pq.type = 'Q';
I am trying to use an Case statement in a MySQL select query.
I am getting an error (Subquery returns more than 1 row)
SELECT mony.come,mony.go,mony.details,mony.id_bill,
(
case mony.details
when 'collect' then (SELECT collect_from_customer.num FROM collect_from_customer INNER JOIN mony ON mony.id_bill = collect_from_customer.id WHERE collect_from_customer.id=mony.id_bill )
when 'pay_to_cust' then (SELECT pay_to_customer.num FROM pay_to_customer INNER JOIN mony ON mony.id_bill = pay_to_customer.id WHERE pay_to_customer.id=mony.id_bill )
end
) as idd
,mony.date FROM mony
please help me
thanks i found solution
SELECT m.come,m.go,m.details,m.id_bill,
(
case m.details
when 'collect' then (SELECT collect_from_customer.num FROM collect_from_customer INNER JOIN mony m1 ON m1.id_bill = collect_from_customer.id WHERE collect_from_customer.id=m.id_bill LIMIT 1 )
when 'pay_to_cust' then (SELECT pay_to_customer.num FROM pay_to_customer INNER JOIN mony m1 ON m1.id_bill = pay_to_customer.id WHERE pay_to_customer.id=m.id_bill LIMIT 1 )
end
) as idd
,m.date FROM mony m
Is there a limit of subquerys where the referencing outer column is working on?
I tried this query:
SELECT
`userouter`.`id` AS `user_id`
FROM
`users` AS `userouter`
WHERE
123456 = (
SELECT
SUM(`tmp`.`sum`) AS `sum_total`
FROM
(
SELECT
SUM(`invoiceposition`.`number` * `invoiceposition`.`amount`) AS `sum`
FROM
`invoices` AS `invoice` INNER JOIN
`invoicepositions` AS `invoiceposition` ON
`invoice`.`id` = `invoiceposition`.`invoice`
WHERE
`invoice`.`user` = `userouter`.`id`
GROUP BY
`invoice`.`id`
) AS `tmp`
)
GROUP BY
`userouter`.`id`
And i get Error Code: 1054. Unknown column 'userouter.id' in 'where clause'
How can i reference the userouter.id in the sub-sub query?
As it seems in a normal way not possible to resolve the problem (double nested subquery reference to outer query), i now solved the problem by creating a mysql function with the user id as parameter.
hope this will help other searchers
Remove the double nesting.
SELECT
`userouter`.`id` AS `user_id`
FROM
`users` AS `userouter`
LEFT JOIN (
SELECT
`invoice`.`user` as `user_id`, SUM(`invoiceposition`.`number` * `invoiceposition`.`amount`) AS `sum`
FROM
`invoices` AS `invoice` INNER JOIN
`invoicepositions` AS `invoiceposition` ON
`invoice`.`id` = `invoiceposition`.`invoice`
WHERE
`invoice`.`user` = `userouter`.`id`
GROUP BY
`invoice`.`id`
) AS `tmp`
ON `tmp`.`user_id` = `userouter`.`id`
WHERE
123456 = `userouter`.`id`
GROUP BY
`userouter`.`id`
I'm looking for faster way to run this kind of statment:
SELECT *
FROM `aukcje_przedmioty`
WHERE (
(
opis NOT
IN (
SELECT opis
FROM aukcje_przedmioty
WHERE ( aktywne =1 AND user_id =6 )
)
)
AND aktywne =0
AND user_id =6
)
table aukcje_przedmioty
you can try something like
SELECT a.*
FROM `aukcje_przedmioty` a
JOIN
(
SELECT opis,user_id
FROM aukcje_przedmioty
GROUP BY opis,user_id
HAVING max(aktywne) = 0
) x
ON
x.user_id = a.user_id and
x.opis = a.opis
WHERE user_id = 6
http://sqlfiddle.com/#!2/16774/6
Use explain on your setup to see what would work best for you.
Im using this query:
SELECT `projects`.*,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`
INNER JOIN `bids` ON `bids`.`id` = `projects`.`bids_id`
HAVING `bids`.`amount` >= `payed`
i get this error: Unknown column 'bids.amount' in 'having clause
But if i change the code to this:
SELECT `projects`.*, `bids`.`amount`,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`
INNER JOIN `bids` ON `bids`.`id` = `projects`.`bids_id`
HAVING `bids`.`amount` >= `payed`
the problem get solved but i do not want to use Select bids.amount
Use a derived table e.g. something like this:
SELECT `DT1`.*
FROM (SELECT `projects`.*,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`) AS `DT1`
INNER JOIN `bids` ON `bids`.`id` = `DT1`.`bids_id`
WHERE `bids`.`amount` >= `DT1`.`payed`;