SQL Syntax error when using cases inside a SELECT - mysql

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

Related

what is wrong with this sql query that used order by and left join together? Is this the right way to do it?

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.

SQL: SUM and Update query error,

I'm trying to update my riders total ucipoints by following an example, with some small modifications, but I just get an error from it.
example
UPDATE P
SET extrasPrice = t.TotalPrice
FROM BookingPitches AS P INNER JOIN
(
SELECT
PitchID,
SUM(Price) TotalPrice
FROM
BookingPitchExtras
GROUP BY PitchID
) t
ON t.PitchID = p.ID
I got the code from this answer:
SQL Update to the SUM of its joined values
My code looks like this:
UPDATE P
SET ucipoeng = t.TotalPoints
FROM rytterlagsesong AS P INNER JOIN
(
SELECT
rytterid,
SUM(poeng) AS TotalPoints
FROM
t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t
ON t.rytterid = P.rytterid AND t.sesong='2016'
I get the error:
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 'FROM rytterlagsesong AS P INNER JOIN ( SELECT rytterid, SUM(ucip' at line 3
Can someone help me find the error?
DB Structure:
rytterlagsesong: rytterid - ucipoeng - sesong
t_ucipoeng: rytterid - dato - poeng
So I want to sum the points (poeng) of all races in 2016 (dato=date) for a rider
And update that riders totalpoint (ucipoeng) for this season (sesong)
The update / join syntax is different per database. For mysql, use this:
UPDATE rytterlagsesong r
INNER JOIN (
SELECT rytterid, SUM(poeng) AS TotalPoints
FROM t_ucipoeng
WHERE year(dato)='2016'
GROUP BY rytterid
) t ON t.rytterid = r.rytterid AND t.sesong='2016'
SET r.ucipoeng = t.TotalPoints

LEFT JOIN sub-SELECT fails

I try to select the the rows with the newest timestamp in change_date from a table in a LEFT JOIN. I really don't know why this query fails:
SELECT
i.ID, i.title, i.create_date,
u1.username creator_name,
u2.username assignee
FROM item i
LEFT JOIN user u1 ON u1.login_IDFK = i.creator_IDFK
LEFT JOIN user u2 ON u2.login_IDFK = i.assigned_to_IDFK
LEFT JOIN (
SELECT MAX(change_date), item_IDFK FROM item_state GROUP BY item_IDFK
) AS ist ON ist.item_IDFK = i.ID
I get the following 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 ') AS ist ON ist.item_IDFK = i.ID' at line 2 (Code: 1064)
Query works great without the last LEFT JOIN
(SELECT change_date, item_IDFK FROM item_state GROUP BY item_IDFK)
You are using a group by clause without an aggregate. Each item in the select list must either be represented in the group by clause, or be part of an aggregate expression
I.E.
(Select Max(Change_Date), item_IDFK from item_state group by item_IDFK)
try to save your last subquery in a view table, and after that, left join from that table, and see if the syntax error persists.

Query error when testing in Database SQL

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

MySQL Join clause in Update

When trying to perform this query:
>mysql_query("UPDATE contracts
SET x = '1'
FROM contracts
INNER JOIN employees
ON contracts.contract_employeeid=employees.employee_id
WHERE experience >= '6'") or die(mysql_error());
I get the following error message:
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 contracts INNER JOIN employees ON contracts.contract_employeeid=employees.employee_id WHERE experience >= '6'
In words, I need to set x=1 on the table "contracts" for the employees who have more than 6 years of experience (to do so, I need to join with the table "employees" on employee_id=contract_employeeid since their experience is stored in that table)
You can probably move the JOIN part of your query before the SET statements:
UPDATE contracts
INNER JOIN employees ON contracts.contract_employeeid = employees.employee_id
SET x = '1'
WHERE experience >= '6'
UPDATE contracts, employees
SET contracts.x = '1'
WHERE contracts.contract_employeeid=employees.employee_id
AND employees.experience >= '6'