MYSQL count Query error - mysql

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.

Related

Update a Select Query

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]+')

sql update with inner join and where

UPDATE newsreactions
SET newsreactions.enabled = '0'
FROM newsreactions
INNER JOIN users ON newsreactions.memberId = users.id
WHERE users.active = '0' AND users.comment LIKE '%spam%'
For some reason I'm getting a syntax 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 'FROM newsreactions INNER JOIN users ON newsreactions.memberId = users.id WHERE u' at line 3
Can't figure it out though.
If I replace the update and set by a select it works fine.
Error 1064 is a MySQL syntax error. The correct MySQL syntax is:
UPDATE newsreactions nr INNER JOIN
users u
ON nr.memberId = u.id
SET nr.enabled = 0
WHERE u.active = 0 AND u.comment LIKE '%spam%';
Notes:
The JOIN goes in the UPDATE clause.
Table aliases makes the query easier to write and to read.
I am guessing that enabled and active are really numeric values. If so, do not use single quotes.
The join clause should come before the set clause, and there should be no from clause in MySQL:
UPDATE newsreactions
JOIN users ON newsreactions.memberId = users.id
SET newsreactions.enabled = '0'
WHERE users.active = '0' AND users.comment LIKE '%spam%'

Querying data with list of ids from different table

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'

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

Error 1064 for no apparently reason

I got this query:
SELECT companies_comments.id as id,
companies_comments.post as post,
companies_comments.comment as comment,
companies_comments.date as date,
companies.`id` AS company,
companies.`name` as name,
companies.`username` as username,
companies.`photo` as photo,
companies.`status` AS company_status
LEFT JOIN companies ON companies.id = companies_comments.company
WHERE company_status NOT IN (3,4)
AND companies_comments.post =1
The 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 'LEFT JOIN companies ON companies.id = companies_comments.company WHERE compa' at line 10
Error 1064 unexpectedly. Just tried with no ` , same results. No missing column. What can be happening?
You're missing the tablename and also a FROM section in the query
SELECT companies_comments.id as id,
companies_comments.post as post,
companies_comments.comment as comment,
companies_comments.date as date,
companies.`id` AS company,
companies.`name` as name,
companies.`username` as username,
companies.`photo` as photo,
companies.`status` AS company_status
FROM companies, companies_comments
LEFT JOIN companies ON companies.id = companies_comments.company
WHERE companies.company_status NOT IN (3,4)
AND companies_comments.post =1
You haven't added FROM what table.You have missed the from part.
Example:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name