Mysql Error Code 1064 - mysql

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).

Related

MySQL Workbench Query returns 1 row, but same query in Jdbc returns 0

I wrote a query that returns 1 row when I run it in MySQL Workbench:
SELECT lot_num, block_num, base_price, SUM(price) AS options_price FROM lots AS l
INNER JOIN models AS m ON l.model_id = m.id
INNER JOIN lot_options AS lo ON l.id = lo.lot_id
INNER JOIN options AS o ON lo.option_id = o.id
GROUP BY l.id
When I try to run that in Jdbc I am getting 0 results. Here is how I have formatted the string for Jdbc:
SELECT lot_num, block_num, base_price, SUM(price) AS options_price FROM lots AS l INNER JOIN models AS m ON l.model_id = m.id INNER JOIN lot_options AS lo ON l.id = lo.lot_id INNER JOIN options AS o ON lo.option_id = o.id GROUP BY l.id;
This does not provide me with an error or anything. Just an empty result set.
I cannot figure out where I am going wrong with this query! Please bestow your SQL mastery upon me!
I figured it out. I didn't know enough about JOIN statements. The many-to-many tables need LEFT OUTER joins. I have no idea why my original query worked in Workbench, but not in my Jdbc connection. This works in both!
SELECT lot_num, block_num, models.base_price, SUM(o.price) FROM lots
INNER JOIN models ON models.id = lots.model_id
LEFT OUTER JOIN lot_options AS lo ON lots.id = lo.lot_id
LEFT OUTER JOIN options AS o ON lo.option_id = o.id
GROUP BY lots.id

Fetching a price with INNER JOIN using 3 different variables

What I have been trying to achieve is to pull a price based on 3 different variables: brand_id, model_id, motor_id.
The design of the tables that I am working with:
The only issue is that when I add an INNER JOIN for databaseapp_lkp_prices I get a zero result set (fyi, there aren't any prices currently set so databaseapp_lkp_prices is an empty table)
I was expecting to see NULL in place of the price for the 48,000 records that exist when I don't add the databaseapp_lkp_prices INNER JOIN
My query is:
SELECT
a.brand,
b.model,
c.motor,
d.ecu_hardware_ver,
d.ecu_software_ver,
d.ecu_software_upg_ver,
d.ecu_brand,
d.ecu_type,
d.eprom,
d.eprom_desc,
d.`checksum`,
d.checksum16,
c.motor_hp * 1.2 AS motor_hp,
e.price,
c.motor_id,
c.model_id,
c.brand_id
FROM
databaseapp_brand AS a
INNER JOIN databaseapp_model AS b ON b.brand_id = a.brand_id
INNER JOIN databaseapp_motor AS c ON b.model_id = c.model_id
INNER JOIN databaseapp_ecu AS d ON d.motor_id = c.motor_id
INNER JOIN databaseapp_lkp_prices AS e ON c.brand_id = e.brand_id AND c.model_id = e.model_id AND c.motor_id = e.motor_id
ORDER BY
a.brand ASC,
b.model ASC
Anyone able to help me out with why I'm getting a zero result set when I try to look up the price.
Cheers!
Inner join requires match. No match no row. You should try LEFT JOIN instead of INNER JOIN for databaseapp_lkp_prices contribution.

Count matched words from IN operator

i have this little mysql query :
select t.title FROM title t
inner join movie_keyword mk on mk.movie_id = t.id
inner join keyword k on k.id = mk.keyword_id
where k.keyword IN (
select k.keyword
FROM title t
inner join movie_keyword mk on mk.movie_id = t.id
inner join keyword k on k.id = mk.keyword_id
where t.id = 166282
)
LIMIT 15
as you can see it will return all titles from title that have at least one the same keyword that have movie with id 166282.
Now i have problem, because i want also count how many keywords was matched in IN operator(let's say i want to see only titles that have 3 or more the same keywords), i tried something with aggregate functions, but everything failed, so i came here with my problem. Maybe somebody can give me some advice, or code example.
I'm not also sure, if this "subquery way" is good, so if there are some better options how i should solve my problem, I am open to any suggestions or tips.
Thank you!
#Edit
So after some problems, i have one more. This is my current query :
SELECT s.title,s.vote,s.rating,count(dk.key) as keywordCnt, count(dg.name) as genreCnt
FROM series s
INNER JOIN series_has_genre shg ON shg.series_id = s.id
INNER JOIN dict_genre dg ON dg.id = shg.dict_genre_id
INNER JOIN series_has_keyword shk ON shk.series_id = s.id
INNER JOIN dict_keyword dk ON dk.id = shk.dict_keyword_id
WHERE dk.key IN (
SELECT dki.key FROM series si
INNER JOIN series_has_keyword shki ON shki.series_id = si.id
INNER JOIN dict_keyword dki ON dki.id = shki.dict_keyword_id
WHERE si.title LIKE 'The Wire'
)
and dg.name IN (
SELECT dgo.name FROM series so
INNER JOIN series_has_genre shgo ON shgo.series_id = so.id
INNER JOIN dict_genre dgo ON dgo.id = shgo.dict_genre_id
WHERE so.title LIKE 'The Wire'
)
and s.production_year > 2000
GROUP BY s.title
ORDER BY s.vote DESC, keywordCnt DESC ,s.rating DESC, genreCnt DESC
LIMIT 5
Problem is, it is very, very, very slow. Any tips what i should change, to run it faster ?
Will this work for you:
select t.title, count(k.keyword) as keywordCount FROM title t
inner join movie_keyword mk on mk.movie_id = t.id
inner join keyword k on k.id = mk.keyword_id
where k.keyword IN (
select ki.keyword
FROM title ti
inner join movie_keyword mki on mki.movie_id = ti.id
inner join keyword ki on ki.id = mki.keyword_id
where ti.id = 166282
) group by t.title
LIMIT 15
Note that I have changed the table names inside the nested query to avoid confusion.

#1066 - Not unique table/alias: 'tb_X'

I got a problem when I was trying to join 3 tables.
Below are the mysql syntax i was using :
SELECT
FROM carecell
LEFT JOIN staff ON cc.id_pks = staff.id_emp
LEFT JOIN staff ON cc.id_wpks = staff.id_emp
INNER JOIN member ON member.id_member = staff.id_member
Please Help me.. What should I do in order to fix the syntax?
The SQL engine cannot distinguish between the two staff tables in the from clause. You need to give them different names. Something like this:
FROM carecell cc LEFT JOIN
staff s1
ON cc.id_pks = s1.id_emp LEFT JOIN
staff s2
ON cc.id_wpks = s2 .id_emp INNER JOIN
member m
ON m.id_member = s2.id_member
If you join the same table multiple times you need to give that table each time a different alias name so the DB engine can differ between them
SELECT *
FROM carecell as cc
LEFT JOIN staff as s1 ON cc.id_pks = s1.id_emp
LEFT JOIN staff as s2 ON cc.id_wpks = s2.id_emp
INNER JOIN member as m ON m.id_member = s1.id_member
You already used aliases since you gave carecell the alias cc. In the last line of your query I joined s1 but you have to decide which one to take - s1 or s2
SELECT *
FROM carecell
LEFT JOIN staff t1 ON cc.id_pks = t1.id_emp
LEFT JOIN staff t2 ON cc.id_wpks = t2.id_emp
INNER JOIN member ON member.id_member = t1.id_member

Error: Unknown Column in 'field list'

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