Rediculous but I cant find error in this request
SELECT * FROM diploms
LEFT JOIN student ON diploms.student_id = student.student_id
LEFT JOIN group ON student.group_id = group.group_id
LEFT JOIN speciality ON group.speciality_id = speciality.speciality_id
ORDER BY (CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END), speciality.name ASC
But SQL says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group ON student.group_id = group.group_id LIMIT 0, 30' at line 3
WTH?
group is a reserved keyword in MySQL and needs to be escaped by backticks.
SELECT *
FROM diploms
LEFT JOIN student ON diploms.student_id = student.student_id
LEFT JOIN `group` ON student.group_id = `group`.group_id
LEFT JOIN speciality ON `group`.speciality_id = speciality.speciality_id
ORDER BY CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END,
speciality.name ASC
Its not good to use reserved keyword as a table or a column name..
group is a reserved keyword and that's why giving an error, you can use quotes tilt (`) to use it as a table name.
Also you can't use it as column name, see the related post:
group as a column name in insert command
Related
SELECT ts.contact_id as contact_id, ts.contact_name as name , ts.is_contact_active as is_active, ts.created_at,ts.updated_at
FROM tb_summary ts
ORDER by created_at
LEFT JOIN (tcs.contact_address as address FROM tb_contacts tcs) ON tcs.contact_id = ts.contact_id
WHERE ts.business_owner_id = ? AND ts.is_contact_active = ?
This query throws
ER_PARSE_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 'LEFT JOIN tcs.Contact_address as address, tcs.Contact_id FROM tb_Contacts tcs ' at line 2
Your syntax is way off. A query takes only one from clause, that may contain multiple joins. And the order by clause goes at the end.
So:
SELECT
ts.contact_id as contact_id,
ts.contact_name as name,
ts.is_contact_active as is_active,
ts.created_at,ts.updated_at,
tc.contact_address as address
FROM tb_summary ts
LEFT JOIN tb_contacts tc ON tc.contact_id = ts.contact_id
WHERE ts.business_owner_id = ? AND ts.is_contact_active = ?
ORDER by ts.created_at
A SELECT statement consists of a sequence of clauses. Your query has four clauses:
SELECT
FROM
WHERE
ORDER BY
These operators must be in this order.
JOIN is an operator in the FROM clause. So, it should be structure more like this:
SELECT ts.contact_id as contact_id, ts.contact_name as name, ts.is_contact_active as is_active, ts.created_at, ts.updated_at
FROM tb_summary ts LEFT JOIN
tcs.contact_address address tca
ON tca.contact_id = ts.contact_id
WHERE ts.business_owner_id = ? AND ts.is_contact_active = ?
ORDER by created_at
From what I can tell, the query is still non-sensical. The contact_address table is not being used.
I am trying to have an if else condition in this select statement. I am either passing a customerID or a accountID into this mapper and thus I need to check which one is being passed. I had the first case where there was no customerId (thus accountId is passed), and so the inner join is performed. The second one is when there is no accountID (thus customerId was passed). Then at the end of the cases, I want to order it by descending dates.
SELECT i.invoice_id, i.account_id, total_amount_due, due_date, bp.eff_date, bp.end_date, total_amount_due
(
CASE
WHEN customerId = null
THEN INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id
WHERE account_id=#{accountId}
ELSE INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id
INNER JOIN server.cust_acct_rel car ON car.account_id = i.account_id
WHERE car.customer_id=#{customerId}
END)
ORDER BY end_date DESC
However, when I run this, I get this error.
org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: java.sql.SQLSyntaxErrorException: 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 'INNER JOIN server.bill_periods bp ON bp.invoice_id = i.invoice_id WHERE ' at line 5
Does anyone know what I am doing wrong here?
use IFNULL
select ...,..., IFNULL(customerID,accountID) newid
from
table1
left outer join table2
order by date desc
I have this query and appearently it's faulty?
I'm trying to join fices with mems so I can have the ficeID along with all the results from mems(These queries work individually). What am I doing wrong?
SELECT *
FROM mems
WHERE deleted <> -1
ORDER BY sort_mems
LEFT JOIN SELECT ficeID
FROM fices
Result:
#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 ficeID FROM offices LIMIT 0, 30' at line 1
You have JOIN clause after ORDER BY. You should place it in FROM.
I suggest you define condition of a LEFT JOIN
Also, I suggest you surround you temp tables with brackets:
SELECT m.*, t1.officeID
FROM members m
LEFT JOIN offices t1
ON m.memberID = t1.memberID
WHERE m.deleted <> -1
ORDER BY m.sort_membername;
Yes, you have the LEFT JOIN in the wrong spot (it should go after your FROM clause, and you also seem to be missing a join criteria (the ON part, this tells the database how these tables are related):
SELECT *
FROM mems m
LEFT JOIN fices f
ON m.??? = f.???
WHERE deleted <> -1
ORDER BY sort_mems
Im having issues trying to figure out this syntax error. Heres the SQL query:
SQL QUERY
SELECT oh.date_modified, oh.physicianNote, os.name AS status
FROM order oh
LEFT JOIN order_status os ON oh.order_status_id = os.order_status_id
WHERE oh.order_id = '118' AND os.language_id = '1'
ORDER BY oh.date_added ASC LIMIT 0,10
SQL 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 'order oh LEFT JOIN order_status os ON oh.order_status_id =
os.order_status_i' at line 2
Not really sure whats wrong with it.
ORDER is a reserved word. Quote it in backticks:
SELECT oh.date_modified, oh.physicianNote, os.name AS status
FROM `order` oh LEFT JOIN order_status os USING (order_status_id)
WHERE oh.order_id = '118' AND os.language_id = '1'
ORDER BY oh.date_added
LIMIT 0,10
"ORDER" is reserved word. This is error. Use the word order Quote in backticks
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