Error 1064 for no apparently reason - mysql

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

Related

I am using the WHERE clause with OR but i am getting an error

I have looked online and have followed others advice such as using back-ticks and using parenthesis but im still getting this error. if anyone can help id appreciate it.
my query:
select p.*, a.street1, a.street2, a.cityeet1, a.street2, a.city, a.name
from person p
left join address a
where (a.name = 'New Jersey' or a.name = 'Connecticut');
error:
ERROR 1064 (42000): 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 'where (a.name = 'New Jersey' or a.name = 'Connecticut')' at line 1
You need to put the join values - possibly something like this:
join address a ON (p.address_id = a.id)
select p.*, a.street1, a.street2, a.cityeet1, a.street2, a.city, a.name from person p left join address a ON (p.address_id = a.id) where (a.name = 'New Jersey' or a.name = 'Connecticut');
Where the two fields join the tables (indicate the matching values)
Also, it looks like you have a typo in the field names (a.cityeet1)

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 : 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

Searching SQL query after using INNER JOIN

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

Compare Subquery to list of elements

I want to select all posts that match a list of tag Id's. I tried the following:
SELECT * FROM posts as t WHERE (SELECT tag_id FROM post_tags WHERE post_tags.post_id = t.id) = ALL (8, 1)
This gives the following error:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 '8, 1))'
I would be glad if someone knows how to solve this.
SELECT p.*
FROM posts p
JOIN post_tags pt
ON pt.post_id = p.id
WHERE tag_id IN(1,8)
GROUP
BY p.id
HAVING COUNT(*) = 2
(where '2' is equal to the number of items in IN()).