The following mysql query...
SELECT a.*, b.*,
(
SELECT COUNT( * )
FROM lp_units c
WHERE c.property_id = a.property_id
) AS unitcount
FROM lp_property a,
lp_property_confidential b
WHERE a.property_id = b.property_id
AND c.unitcount<= a.no_of_units
AND a.account_id = '1'
returns an error...
Unknown column 'c.unitcount' in 'where clause'
I think my query would be understandable. solve it to run....
Thanks in advance...
Don't use c.unitcount. Just unitcount. unitcount is not a column of c but rather of the temporary table generated by the subquery.
However, this query is probably better written as a join anyway.
Try this query
SELECT
a.*,
b.*,
COUNT(c.property_id) as unitcount
FROM lp_property a
JOIN lp_property_confidential b ON a.property_id = b.property_id
JOIN lp_units c ON c.property_id = a.property_id
WHERE
a.account_id = '1'
GROUP BY a.property_id
HAVING unitcount <= a.no_of_units
Related
I'm trying to get the user with oldest account created using this:
SELECT a.ID
, a.username
FROM users a
JOIN
( SELECT MAX(date_created)
FROM other_info
) b
ON a.ID = b.ID;
It's returning the following; `Error Code: 1054.
Unknown column 'b.ID' in 'on clause'`
I've looked though the [mysql-error-1054] tag and haven't been able to find a solution, I'm not even sure which part of the query is wrong.
Thanks.
To find the user with the first (oldest) date_created:
SELECT u.ID, a.username, b.date_created
FROM users a
INNER JOIN other_info b
ORDER BY b.date_created
LIMIT 1
BTW, instead of a, b etc, I'd rather chose table aliases that make sense, e.g. u for users.
SELECT a.ID
, a.username
, MAX(date_created)
FROM users a
INNER JOIN other_info b
ON a.ID = b.ID;
Try This :
SELECT a.ID, a.username
FROM users a INNER JOIN
(SELECT top 1 id,date_created FROM other_info order by date_created desc) b
ON a.ID = b.ID;
I have this query:
SELECT *,
COALESCE((
SELECT SUM(sum_points)
FROM (
SELECT SUM(user_points.points) AS sum_points FROM user_points
WHERE user_points.user_id = user.user_id
UNION ALL
SELECT SUM(used_points.points) AS sum_points FROM used_points
WHERE used_points.user_id = user.user_id
) t
), 0) AS total_points_credit
FROM user
But i get this error:
#1054 - Unknown column 'user.user_id' in 'where clause'
How can i user the user.user_id value in WHERE condition in the subqueries?
Thank you.
Try this way:
SELECT *,
COALESCE((SELECT SUM(user_points.points)
FROM user_points
WHERE user_points.user_id = user.user_id), 0)
+
COALESCE((SELECT SUM(used_points.points)
FROM used_points
WHERE used_points.user_id = user.user_id), 0) AS total_points_credit
FROM user
I don't think that you need to have all of these subqueries. I feel like you can likely do what you're looking to do with:
SELECT A.user_id, SUM(B.points) + SUM(C.points) total_points_credit FROM user A
JOIN user_points B ON
A.user_id = B.user_id
JOIN used_points C ON
A.user_id = C.user_id
GROUP BY A.user_id
I get the error below:
ER_BAD_FIELD_ERROR: Unknown column 'c.id' in 'where clause':
SELECT *
FROM clients c
LEFT JOIN
(SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
WHERE client_id = c.id) AS l ON
l.client_id = c.id
I need use WHERE to group smpp_user columns for each c.id from main SELECT.
Help please? I believe it's possible.
Just remove WHERE clause in your sub query and use GROUP BY:
SELECT *
FROM clients c
LEFT JOIN (
SELECT GROUP_CONCAT(smpp_user), client_id
FROM client_accounts
GROUP BY client_id
) AS l ON l.client_id = c.id
I have this query and it is working fine as expected, ie, it shows all the columns as well as the 2 columns from each sub-query...
SELECT
a.group_id,
a.code,
a.description_en,
c.size_code,
(
SELECT
SUM(b.qty)
FROM
receiving_details AS b
WHERE
b.code = c.size_code
) AS in_qty,
(
SELECT
SUM(d.qty)
FROM
requisition_details AS d
WHERE
d. matl_code = c.size_code
) AS out_qty
FROM products AS a
INNER JOIN products_sizes AS c ON c.prod_code = a.code
ORDER BY a.group_id ASC, a.code ASC, c.size_code ASC
However, when I try to add this line...
(in_qty - out_qty) AS balance,
just before the 'FROM' statement, I get an error of Unknown column 'in_qty' in 'field list'.
What am I doing wrong?
EDIT:
From the accepted answer, I did a few more fix and got the result I wanted.
SELECT *, (e.in_qty - e.out_qty) AS balance FROM
(SELECT
a.group_id,
a.code,
a.description_en,
c.size_code,
(
SELECT
IFNULL(SUM(b.qty),0)
FROM
receiving_details AS b
WHERE
b.code = c.size_code
) AS in_qty,
(
SELECT
IFNULL(SUM(d.qty),0)
FROM
requisition_details AS d
WHERE
d. matl_code = c.size_code
) AS out_qty
FROM products AS a
INNER JOIN products_sizes AS c ON c.prod_code = a.code) AS e
ORDER BY e.group_id ASC, e.code ASC, e.size_code ASC
You can't reference the same field from your select statement within itself. One option would be to move the results in another subquery, and then perform the calculation:
select *, (in_qty - out_qty) AS balance
from (
SELECT
a.group_id,
a.code,
a.description_en,
c.size_code,
(
SELECT
SUM(b.qty)
FROM
receiving_details AS b
WHERE
b.code = c.size_code
) AS in_qty,
(
SELECT
SUM(d.qty)
FROM
requisition_details AS d
WHERE
d. matl_code = c.size_code
) AS out_qty
FROM products AS a
INNER JOIN products_sizes AS c ON c.prod_code = a.code
) t
ORDER BY group_id, code, size_code
I cannot solve this issue, I have tree tables, inner join and I'm after selecting only specific columns because I'm concerned about performance issue if I just leave SELECT * to do the job.
I have tables: ugovori-artikli, ugovori and ids, working SELECT * query:
SELECT * FROM `ugovori-artikli`
INNER JOIN `ugovori`
ON `ugovori-artikli`.`ugovor_id` = `ugovori`.`id`
INNER JOIN `ids`
ON `ugovori`.`kupac_id` = `ids`.`id`
AND `ugovori-artikli`.`artikal` = ?
and non-working query, my attempt:
SELECT a.*,
b.*,
c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b
ON `ugovori-artikli`.`ugovor_id` = `ugovori`.`id`
INNER JOIN `ids` AS c
ON `ugovori`.`kupac_id` = `ids`.`id`
AND `ugovori-artikli`.`artikal` = ?
I get an error:
Error in query (1054): Unknown column 'ugovori-artikli.ugovor_id' in 'on clause'
Use the alias name
SELECT a.*,
b.*,
c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b
ON a.`ugovor_id` = b.`id`
INNER JOIN `ids` AS c
ON b.`kupac_id` = c.`id`
AND a.`artikal` = ?
Hope this helps :)
You have to use the aliasses not the table name in your SQL statement.
SELECT a.*,b.*,c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b ON a.`ugovor_id` = b.`id`
INNER JOIN `ids` AS c
ON b.`kupac_id` = cid`
AND a.`artikal` = ?