Update a Select Query - mysql

I am trying to get rid of all the NULL values and replace them with an = sign in the column dimension_prefix from the results of the query. I keep getting a SYNTAX error though, I can't figure out where I'm wrong. Any help Is much appreciated. Thanks
I am getting 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 'IN( >SELECT dimension_prefix,length,width,height,ovd.name,p.`product_i' at >line 1
UPDATE oc_product_option_value_dimension SET dimension_prefix = '=' WHERE dimension_prefix IS NULL IN(
SELECT `dimension_prefix`,`length`,`width`,`height`,ovd.`name`,p.`product_id`,pov.product_option_value_id
FROM ( SELECT product_id FROM `oc_product` ORDER BY product_id ASC) AS p
LEFT JOIN `oc_product_option_value` pov ON pov.product_id=p.product_id
LEFT JOIN `oc_product_option_value_dimension` povd ON povd.product_option_value_id=pov.product_option_value_id
LEFT JOIN `oc_option_value_description` ovd ON ovd.option_value_id=pov.option_value_id
LEFT JOIN `oc_option_description` od ON od.option_id=ovd.option_id
WHERE ovd.`name` regexp '^[0-9]+')

Actually you are trying is null in .. which is giving you the syntax error.
UPDATE oc_product_option_value_dimension SET dimension_prefix = '='
WHERE coalesce(dimension_prefix,"##") = (
SELECT coalesce(`dimension_prefix`,"##")
FROM ( SELECT product_id FROM `oc_product` ORDER BY product_id ASC) AS p
LEFT JOIN `oc_product_option_value` pov ON pov.product_id=p.product_id
LEFT JOIN `oc_product_option_value_dimension` povd ON povd.product_option_value_id=pov.product_option_value_id
LEFT JOIN `oc_option_value_description` ovd ON ovd.option_value_id=pov.option_value_id
LEFT JOIN `oc_option_description` od ON od.option_id=ovd.option_id
WHERE ovd.`name` regexp '^[0-9]+')

Related

MYSQL count Query error

Mysql Query:
SELECT `message`.`id` as `message_id`,
`pet_info`.`id` as `pet_id`,
`pet_info`.`pet_hidenum` as `hidenum`,
`lostpets`.`pet_lost_date` as `pet_lost_date`,
`lostpets`.`type` as `status`,
`pet_images`.`img` as `img`,
COUNT(SELECT * FROM `message` WHERE `message`.`status` = 'not seen') as unread
FROM `message`
LEFT JOIN `pet_info` ON `pet_info`.`id` = `message`.`pet_id`
LEFT JOIN `pet_images` ON `pet_images`.`petid` = `message`.`pet_id`
LEFT JOIN `lostpets` ON `lostpets`.`petid` = `message`.`pet_id`
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 * FROM `message` WHERE `message`.`status` = 'not seen') as unread FROM `m' at line 1
Please help me where is an error in this query? and How can I resolve this error?
Not sure what you intended, but for the error, you want to apply the count function inside the subquery:
select message.id as message_id,
pet_info.id as pet_id,
pet_info.pet_hidenum as hidenum,
lostpets.pet_lost_date as pet_lost_date,
lostpets.type as status,
pet_images.img as img,
(
select COUNT(*)
from message
where message.status = 'not seen'
) as unread
from message
left join pet_info on pet_info.id = message.pet_id
left join pet_images on pet_images.petid = message.pet_id
left join lostpets on lostpets.petid = message.pet_id
Also try to not use backticks by using standard aliases and identifier names because the backticks hinder readability.
If that's not what you want, please edit your question and add sample data and expected output.

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

SQL : Update table with select

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

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