I'm trying to do something like thisin MySQL. Query on one table with a list of ids from another table. The Group_Concat return a list of comma separated ids that i want to use in my first select. My try results in an error.
(Error Code: 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 'IN ( SELECT GROUP_CONCAT(t.id SEPARATOR ',') FROM m.my_aspnet_detai' at line 2)
How does one write this with correct syntax?
SELECT * FROM m.my_aspnet_membership m
where m.userId = IN
(
SELECT GROUP_CONCAT(t.id SEPARATOR ',')
FROM m.my_aspnet_details t
WHERE t.customerid = '2'
);
No need to GROUP_CONCAT
SELECT * FROM m.my_aspnet_membership m
where m.userId = IN
(
SELECT t.id
FROM m.my_aspnet_details t
WHERE t.customerid = '2'
);
Or even better to use JOIN
SELECT *
FROM m.my_aspnet_membership
INNER JOIN m.my_aspnet_details t ON t.id=m.userId
WHERE t.customerid = '2'
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'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
I have a query like this
UPDATE t_prd_cost_compare
SET
2015_AUG_PRD_UNIT_PRICE=i.PRD_UNIT_PRICE,
2015_AUG_PRD_SELLING_PRICE=i.PRD_SELLING_PRICE,
2015_AUG_PRD_IN_PATIENT_LIST_PRICE=i.PRD_IN_PATIENT_LIST_PRICE,
2015_AUG_PRD_OUT_PATIENT_LIST_PRICE=i.PRD_OUT_PATIENT_LIST_PRICE
FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRICE,PRD_OUT_PATIENT_LIST_PRICE
FROM t_product_catalog
LEFT JOIN T_adjust ON IAJ_PRODUCTID=PRODUCTID AND IAJ_ADJNO IS NULL
WHERE PRODUCTID>1 AND (DATE(IAJ_DATE) = '2015-01-01')
GROUP BY IAJ_PRODUCTID
) AS i
WHERE i.PRODUCTID = t_prd_cost_compare.PRODUCTID
I get error like this
Error Code: 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 'FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRI' at line 7
I done checked the select statement is correct, but I still get error!
Any idea?
Issue solved, here is the solution
Update
Competition as C
inner join (
select CompetitionId, count(*) as NumberOfTeams
from PicksPoints as p
where UserCompetitionID is not NULL
group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = A.NumberOfTeams
refer from: mysql update query with sub query
When displaying the table without the search it prints perfectly, when when adding the where query (which works fine in other search tables without inner join included) it produces a syntax error. Here is the code:
SELECT Date_entered, photo1, photo2, UserName, reserveName, species FROM Plant_Reserves
INNER JOIN Plant_Species ON Plant_Reserves.plantID = Plant_Species.plantID
INNER JOIN reserves ON Plant_Reserves.reserveID = reserves.reserveID
ORDER BY UserName WHERE UserName LIKE '%$search%'
Here is a copy of the 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 'WHERE UserName LIKE '%zz%'' at line 4
where comes before order by
SELECT Date_entered, photo1, photo2, UserName, reserveName, species
FROM Plant_Reserves
INNER JOIN Plant_Species ON Plant_Reserves.plantID = Plant_Species.plantID
INNER JOIN reserves ON Plant_Reserves.reserveID = reserves.reserveID
WHERE UserName LIKE '%$search%'
ORDER BY UserName
The defined order of keywords is
select
from
join
where
group by
having
order by
limit
getting this error:
Error Code: 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 'rMin left join select * from ( ( select symbol,(dt) as dt,callPut,bid,ask,stri' at line 15
on the following query:
select * from (
select * from
(
(
select
symbol,(dt) as dt,callPut,bid,ask,strike,maturity,`Open Int`,impVol,theta,delta from bats.Opt where impVol>0
) as a1
inner join
(select
symbol,min(dt) as dt from bats.Opt where impVol>0
group by symbol
) as b1
on a1.symbol=b1.symbol and a1.dt=b1.dt
)
as rMin
left join
select * from
(
(
select
symbol,(dt) as dt,callPut,bid,ask,strike,maturity,`Open Int`,impVol,theta,delta from bats.Opt where impVol>0
) as a1
inner join
(select
symbol,max(dt) as dt from bats.Opt where impVol>0
group by symbol
) as b1
on a1.symbol=b1.symbol and a1.dt=b1.dt
)
as rMax
on rMax.symbol=rMin.symbol
)
I would start by formatting the query so you can read it easily, matching parentheses and so on.
Then, I would run each subquery to be sure they are syntactically correct.
But, before doing that, I'd put a table alias after the last closing parentheses. MySQL requires subqueries in the from clause to have table aliases. Admittedly, the error for this is the more descriptive "Every derived table must have its own alias", so this might not solve the entire problem.