Mysql error saying "an expression was expected near FROM" - mysql

SELECT users.name,
phone_info.phone_num,
FROM users LEFT OUTER JOIN phone_info
ON users.user_id = phone_info.user_id
I have 2 tables in my db users and phone_info. I want to use left join to execute the users who have numbers. However I get an error like this
Static analysis:
1 errors were found during analysis.
An expression was expected. (near "FROM" at position 43)
SQL query: Documentation
SELECT users.name, phone_info.phone_num, FROM users LEFT JOIN phone_info ON
users.user_id = phone_info.user_id
MySQL said: Documentation
#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 users LEFT JOIN phone_info
ON users.user_id = phone_info.user_id' at line 3

Get rid of the extra comma before FROM
SELECT users.name,
phone_info.phone_num
FROM users LEFT OUTER JOIN phone_info
ON users.user_id = phone_info.user_id

SELECT users.name,
phone_info.phone_num
FROM users LEFT OUTER JOIN phone_info
ON users.user_id = phone_info.user_id ;I just removed that extra comma .try now I should work

The error explains it self.
An expression was expected. (near "FROM" at position 43);
Because you have entered a comma(,) MySQL expects an expression after that. Just remove it.
Here's the query;
SELECT users.name,
phone_info.phone_num
FROM users LEFT OUTER JOIN phone_info
ON users.user_id = phone_info.user_id;

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 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%'

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

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