mysql left join syntax error 1064 - mysql

Really stumped on this one. It should be a simple left join to a second table but i'm getting a syntax error. Trying to get the last due date on incomplete items.
Code:
SELECT *
FROM TBLTICKETHEADER h,
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) ld ON ld.HEADERID = h.HEADERID
ERROR:
1064 - 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 'LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETIT' at line 3

You have a comma after your h on the from clause. Remove it and your query should run.
SELECT *
FROM TBLTICKETHEADER h
LEFT JOIN (SELECT HEADERID, MAX(DUEDATE)
FROM TBLTICKETITEM
WHERE YEAR(COMPLETEDDATE) = 9999
GROUP BY HEADERID) AS ld ON ld.HEADERID = h.HEADERID

Related

Why does this MySQL error near group by statement?

I want to group my table with mem_num and it appears error saying that :
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 'group by m.mem_num' at line 6.
Here's my code.
select m.mem_num, count(d.vid_num) as total
from membership m
inner join rental r on m.mem_num = r.mem_num
inner join detailrental d on r.rent_num = d.rent_num
having count(d.vid_num) > 1
group by m.mem_num;
For a clearer view click here to view the image
:
output query
HAVING belongs after the GROUP BY
select m.mem_num, count(d.vid_num) as total
from membership m
inner join rental r on m.mem_num = r.mem_num
inner join detailrental d on r.rent_num = d.rent_num
group by m.mem_num
having count(d.vid_num) > 1;

#1064 - You have an error in your SQL syntax: What is wrong with this query?

Please have a look at the below query.
SELECT sub_words.idwords, words_inc.idArticle
(
SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS excl_words, COUNT(sub1.idwords) AS older_words_cnt
FROM words_learned sub0
LEFT OUTER JOIN words_learned sub1
ON sub0.userId = sub1.userId
AND sub0.order < sub1.order
WHERE sub0.userId = 1
GROUP BY sub0.idwords
) sub_words
INNER JOIN words words_inc
ON sub_words.idwords = words_inc.idwords
LEFT OUTER JOIN words words_exc
ON words_inc.idArticle = words_exc.idArticle
AND FIND_IN_SET(words_exc.idwords, sub_words.excl_words)
WHERE words_exc.idwords IS NULL
ORDER BY older_words_cnt
LIMIT 100
This gives the error
#1064 - 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 'SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS exc' at line 3
I checked the sub query individually and there was no error in the sub query! what is going on here?
You missed the from keyword
SELECT sub_words.idwords, words_inc.idArticle
FROM
( ...

SQL query- Different databases with inner join

I have two DBs- RATINGSAPP and MIGRATIONDATA.
I want to update a table in RATINGSAPP with some values in a table in MIGRATIONDATA. I am trying to run this query:
update r set internal_id = m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
This gives me error:
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
'from ratingsapp.hotel03 as r inner join migrationdata.migration as m on r.hotel_' at line 1
But a similar select query works for me and gives proper results.
select r.hotel_id, m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
What I am doing wrong in the update query?
The correct MySQL syntax is:
update ratingsapp.hotel03 r inner join
migrationdata.migration as m
on r.hotel_id = m.restaurant_id
set internal_id = m.internal_id ;
There is no from clause in a MySQL update. You are using SQL Server/Postgres syntax.

What is wrong (#1064) with this MySQL query?

Please can anyone tell me what's wrong with this MySQL query
SELECT count(items.id) AS numrows
FROM items, subcategories
WHERE items.`base_school` = 'UNN'
AND items.subcategory = subcategories.id
AND subcategories.parent_category = 3
JOIN `item_images`
ON items.id=`item_images`.`item_id`
It keeps throwing up this error:
#1064 - 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 'JOIN item_images ON items.id=item_images.item_id' at line
1
You have the WHERE clause in the wrong location, try this:
SELECT count(items.id) AS numrows
FROM items
INNER JOIN subcategories
ON items.subcategory = subcategories.id
INNER JOIN item_images
ON items.id=item_images.item_id
WHERE items.base_school = 'UNN'
AND subcategories.parent_category = 3

MySQL JOIN with WHERE

I have the following query:
SELECT * FROM
(SELECT t1.`id`, t1.`vehicle`, lp1.`veh_no` AS `lp_vehicle`,
t1.`last_date`, t1.`due_date`, t1.`driver`, lp4.`employ_name` AS `lp_driver`
FROM `inspection` AS t1 LEFT OUTER JOIN `vehicle` AS lp1
ON (t1.`vehicle` = lp1.`id`)
LEFT OUTER JOIN `employee_driver` AS lp4
ON (t1.`driver` = lp4.`id`)) subq,
WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER by vehicle asc;
It processes through fine until I get to the WHERE clause.
This is what I get on the above:
#1064 - 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
'WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER b'
at line 1
Can someone please point out what I am doing wrong? I'm running MySQL 5.1.48
you have an extra comma after subq