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
Related
Trying to complete this multi-query assignment but cannot figure out why I keep getting this error code (Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ebe.Co.ContactName' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by). This is my code below
USE EBE;
SELECT C.ClientName, CO.ContactName, CO.ContactPhone, CO.ContactEmail,
COUNT(E.EventCode) AS NumberOfEvents
FROM Client AS C
INNER JOIN Contact AS CO ON C.ClientID = CO.ClientID
INNER JOIN Events AS E ON C.ClientID = E.ClientID
GROUP BY E.ClientID
ORDER BY C.ClientName;
All the columns that are being selected except for the aggregated column need to be in the GROUP BY
SELECT C.ClientName, CO.ContactName, CO.ContactPhone, CO.ContactEmail,
COUNT(E.EventCode) AS NumberOfEvents
FROM Client AS C
INNER JOIN Contact AS CO ON C.ClientID = CO.ClientID
INNER JOIN Events AS E ON C.ClientID = E.ClientID
GROUP BY C.ClientName, CO.ContactName, CO.ContactPhone, CO.ContactEmail
ORDER BY C.ClientName
Another way of writing your query to remove the need for group by would be to use a correlated subquery:
select C.ClientName,
CO.ContactName,
CO.ContactPhone,
CO.ContactEmail,
(select Count(*) from Events E where E.ClientID = C.ClientID) NumberOfEvents
from Client C
join Contact CO on CO.ClientID = c.ClientID
order by C.ClientName;
I would like to display a count of tickets per staff member while showing current tickets.
SELECT s.firstname, s.lastname, t.ticket_id, o.id, t.created FROM ost_ticket t
JOIN ost_staff s ON t.staff_id = s.staff_id
JOIN ost_user u ON t.user_id = u.id
JOIN ost_organization o ON u.org_id = o.id
RIGHT JOIN (
SELECT COUNT(tt.ticket_id)
FROM ost_ticket tt) a ON t.ticket_id = a.ticket_id WHERE...
I'm getting a Error in query (1054):
Unknown column 'a.ticket_id' in 'on clause'.
You need to select ticket_id in the subquery if you want to refer to it on the outside.
RIGHT JOIN
(
SELECT ticket_id, COUNT(ticket_id) as cnt
FROM ost_ticket
GROUP BY ticket_id
) a ON t.ticket_id = a.ticket_id
I need to get the id and timestamps of table sellers and all other columns (without knowing the column names) from these results returned from this MySql statement:
SELECT * FROM sellers a
LEFT JOIN users b ON a.user_id = b.id
LEFT JOIN country_types c ON a.country_type_id = c.id
LEFT JOIN language_types d ON a.language_type_id = d.id
WHERE a.email=?
The seller id though is incorrectly set because users, country_types, and language_types all have a value id. How can I set seller_id and seller_timestamp? I tried this but it is incorrect:
SELECT a.id seller_id, a.timestamp seller_timestamp, * FROM sellers a ...
You want this:
SELECT a.id as seller_id, a.timestamp as seller_timestamp, a.*, b.*, c.*, d.*
FROM sellers a
LEFT JOIN users b ON a.user_id = b.id
LEFT JOIN country_types c ON a.country_type_id = c.id
LEFT JOIN language_types d ON a.language_type_id = d.id
WHERE a.email=?
Im not sure but try alias, for example:
a.id AS seller_id
and etc.
In joins you can't select other columns in this way:
SELECT a.id seller_id, a.timestamp seller_timestamp, * FROM sellers a...
You need to write required column names.
I receive an error that the column ls.amount is in field list when
I run the following query.
Can anyone help me diagnose the problem.
SELECT c.name, ic.keyword, COUNT(ic.keyword), SUM(ls.amount), ls.buyer FROM in_clicks AS ic
INNER JOIN ads AS a ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c ON ag.campaign_id = c.id;
INNER JOIN leads AS l ON (ic.id = l.in_click_id)
INNER JOIN lead_status AS ls ON (l.id = ls.lead_id)
WHERE ic.create_date LIKE '%2011-08-19%' AND ic.location NOT LIKE '%Littleton%' AND discriminator LIKE '%AUTO_POST%'
GROUP BY ic.keyword ORDER BY COUNT(ic.keyword) DESC
The exact error message is:
Error Code: 1054
Unknown column 'ls.amount' in 'field list'
Drop the semicolon (;) on line 4. I suspect that is ending you query before you can define the ls alias.
SELECT c.name,
ic.keyword,
COUNT(ic.keyword),
SUM(ls.amount),
ls.buyer
FROM in_clicks AS ic
INNER JOIN ads AS a
ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag
ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c
ON ag.campaign_id = c.id
INNER JOIN leads AS l
ON ( ic.id = l.in_click_id )
INNER JOIN lead_status AS ls
ON ( l.id = ls.lead_id )
WHERE ic.create_date LIKE '%2011-08-19%'
AND ic.location NOT LIKE '%Littleton%'
AND discriminator LIKE '%AUTO_POST%'
GROUP BY ic.keyword
ORDER BY COUNT(ic.keyword) DESC
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