Error: Unknown Column in 'field list' - mysql

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

Related

Sum two alias column in mysql

I tried to sum two alias column but failed, here's my query:
SELECT MAX(a.`score`) AS score1,SUM(c.`testscore`) AS score2,b.`first_name`,b.`last_name`,b.`picture` ,
SUM(score1 + score2) AS grandtotal
FROM test AS a
INNER JOIN employees AS b
ON a.`id` = b.`id`
INNER JOIN game AS c
ON a.`id` = c.`id`
GROUP BY a.`id`;
Getting error: Unknown column 'score1 ' in 'field list'
Solution:
SELECT MAX(a.`score`) AS score1,SUM(c.`testscore`) AS score2,b.`first_name`,b.`last_name`,b.`picture` ,
(MAX(a.`score`) + SUM(c.`testscore`)) AS grandtotal
FROM test AS a
INNER JOIN employees AS b
ON a.`id` = b.`id`
INNER JOIN game AS c
ON a.`id` = c.`id`
GROUP BY a.`id`;

How to recognize parent alias in Join (SELECT?

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

Inner join 3 tables with only specific columns selected

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` = ?

MySQL SubQuery with query variables

I'm trying to do the following MySql query and when i go to run it, it tells me that "q.object_id" in the sub-query is unknown (if i change it to o.id, it says the same).
SELECT q.*, (SELECT c.title
FROM api_course c
LEFT OUTER JOIN api_object_parents op
ON c.object_id = op.parent
AND op.object_id = q.object_id) as parent_title
FROM api_quiz q
LEFT OUTER JOIN api_object o ON q.object_id = o.id
WHERE o.type = 'Quiz'
Basically i need to get the same id that is being used in the main query and use that in a sub-query.
Thanks!
is this what you are looking for?
SELECT a.*, d.title
FROM api_quiz a
LEFT JOIN api_object b
on a.object_id = b.id
LEFT JOIN api_object_parents c
ON c.parent = a.object_id
LEFT JOIN api_course d
ON c.object_id = c.parent

Mysql Error Code 1064

I'm having some issues with a mysql query.
I'm trying to join together multiple tables.
In one table, I have a list of successful online customers, and on another table I have a table which has the traffic source from which it came. I'm trying to track which customers came from a certain ad. So I want to query those new customers who came on a certain date and from a specific campaign.
SELECT keyword, COUNT(keyword) FROM in_clicks AS ic WHERE ic.create_date LIKE '%2011-08-19%' GROUP BY ic.keyword ORDER BY COUNT(ic.keyword) DESC
INNER JOIN leads AS l ON ls.lead_id = l.id
INNER JOIN lead_status AS ls ON ls.lead_id = ls.id
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;
I ran the above code and got the following error.
Error Code: 1064
'INNER JOIN leads AS l ON ls.lead_id = l.id Innner Join lead_status AS ls' at line 2
This should run better :-).
SELECT keyword, COUNT(keyword)
FROM in_clicks AS ic
INNER JOIN leads AS l ON ls.lead_id = l.id
INNER JOIN lead_status AS ls ON ls.lead_id = ls.id
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;
WHERE ic.create_date LIKE '%2011-08-19%'
GROUP BY ic.keyword
ORDER BY COUNT(ic.keyword) DESC
The order is: SELECT .. FROM ... JOIN ... WHERE ... GROUP BY .. HAVING .. ORDER BY .. LIMIT
All of the INNER JOIN clauses need to be between your FROM and WHERE clauses. It's syntactically invalid to use INNER JOIN following WHERE in the same query.
As pointed out by Bohemian, you also have a syntax error: Innner should be INNER (you have one to many n characters).