Query error when testing in Database SQL - mysql

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

Related

mysql subquery in select join

I have a problem with the following statement:
SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
FROM foreseen_charges GROUP BY foreseen_charges.flatsId
And I always getting this 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 (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON f' at line 2
Could anyone help me?
Best Regards,
Cs
Put FROM before LEFT JOIN
SELECT SUM(foreseen_charges.commonCharge) as required,
foreseen_charges.flatsId
FROM foreseen_charges
LEFT JOIN (
SELECT deposits.flatsId
FROM deposits
GROUP BY flatsId
) deps ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId
You're doing things out of order. The general structure is:
SELECT (something) FROM table JOIN other-table ON table.var1=other-table.var2 WHERE situation
You're doing this:
SELECT (something) LEFT JOIN table ON table.var1=other-table.var2 FROM other-table
You can't join until you've declared both tables, and you can't relate other-table because you haven't declared it yet.
Unsure what the query is meant to accomplish, but this should fix your syntax error:
SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId
FROM foreseen_charges
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId

Constantly getting syntax error after subquery alias (happens with different queries)

It's probably something I can't seem to understand with MySQL, but after wasting my day going through StackOverflow's related questions without fixing the issue, I decided to ask about it.
SELECT users.idUser, users.name, categoryName
FROM users
LEFT JOIN (
SELECT `translation` as categoryName
FROM localization,
usercategories
WHERE localization.`string` = usercategories.name
AND usercategories.idUserCategory = users.idUserCategory
)
as Something
WHERE users.idUser != 1
ORDER BY users.name ASC
Whichever query I tried today that would include a subquery, I would get the same syntax error at pretty much the same place: right after the subquery's alias (in this case, Something).
#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 'WHERE users.idUser != 1 ORDER BY users.name ASC LIMIT 0, 30' at line 11
This issue here is that you're missing the ON clause of your join. You need to select a condition to join the two tables together, like this:
SELECT stuff
FROM stuff
LEFT JOIN (other stuff)
ON stuff.something = otherstuff.something. // Add here.
You're JOIN criteria is non-ANSI and does not have an ON clause ... perhaps that is causing it? Try this, a bit more optimized:
SELECT Usr.idUser AS idUser
,Usr.name AS name
,UsrCat.translation AS categoryName
FROM users AS Usr
LEFT OUTER JOIN usercategories AS UsrCat
ON UsrCat.idUserCategory = Usr.idUserCategory
LEFT OUTER JOIN localization AS Lcl
ON Lcl.string = UsrCat.name
WHERE Usr.idUser <> 1
ORDER BY Usr.name ASC
No need for subquery, should be pretty performant.
You could re-organize your query so that it does not need a sub-query. This would also allow you the benefit of adding more columns to the select from any of the tables. Also, it is more correct.
SELECT
users.idUser,
users.name,
localization.`translation` as categoryName
FROM users
LEFT JOIN usercategories ON usercategories.idUserCategory = users.idUserCategory
LEFT JOIN localization ON localization.`string`= usercategories.name
WHERE users.idUser <> 1
ORDER BY users.name ASC

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.

JDBC with Mysql: using mulitple outer joins with where clauses

I'm having an issue with the following query. It works in mysql workbench but not when I use it in java with jdbc. I keep getting a syntax error.
Here is the query:
"SELECT f.ISBN, f.text, m.title, AVG(r.rating) as score" +
" FROM RATES r LEFT OUTER JOIN FEEDBACK f ON (f.fid = r.fid) WHERE f.ISBN = ? " +
"LEFT OUTER JOIN MOVIE m ON (m.ISBN = f.ISBN) " +
"GROUP BY ISBN " +
"ORDER BY score DESC LIMIT ? ";
I did some searching and found a jdbc escape syntax of {oj }. But I would get another syntax error.
The error I am getting lately is:
HTTP Status 500 - javax.servlet.ServletException:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 OUTER JOIN
MOVIE m ON (m.ISBN = f.ISBN) GROUP BY ISBN ORDER BY score DESC L' at
line 1
I would love some fresh eyes on this since I can't seem to see my issue.
Thanks in advance!
I think that WHERE clause has to be placed behind JOINING. And next problem is that number of columns in select clause has equal to number of columns in group clause(except functions as avg, count etc.). So you need to correct your query like this:
SELECT f.ISBN, f.text, m.title, AVG(r.rating) as score
FROM RATES r
LEFT OUTER JOIN FEEDBACK f ON (f.fid = r.fid)
LEFT OUTER JOIN MOVIE m ON (m.ISBN = f.ISBN)
WHERE f.ISBN = ?
GROUP BY f.ISBN, f.text, m.title
ORDER BY score DESC LIMIT ?
The left join must be before the where clause. And every column listed in the select clause should be in the group by clause:
SELECT f.ISBN, f.text, m.title, AVG(r.rating) as score
FROM RATES r
LEFT OUTER JOIN FEEDBACK f ON (f.fid = r.fid)
LEFT OUTER JOIN MOVIE m ON (m.ISBN = f.ISBN)
WHERE f.ISBN = ?
GROUP BY f.ISBN f.text, m.title
ORDER BY score DESC
LIMIT ?

mysql syntax error

I have the following query :
select irc.*,p.*,#product :='prod_product',#accessrole :='pub_accessrole'
from item_rel_coupon irc
join user_rel_coupon urc on urc.userId = 7 and irc.couponId=urc.couponId
left join if(irc.source='product',#product,#accessrole) as p on p.id=irc.itemId
But I get a syntax error. Why?
Nanne is right, your IF() is not a table. There are two ways around:
You join on both tables, and put the
IF in your select to select the
columns from the table you want.
(recommended)
You use the IF to create the query
in a string, you PREPARE the string
and EXECUTE the handle. (not
recommended)
This part gives an error:1
left join if(irc.source='product',#product,#accessrole) as p on p.id=irc.itemId
I after a JOIN you need a table reference, and I don't think your if 's result is one.
1: 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 'if(irc.source='product',#product,#accessrole) as p on p.id=irc.itemId LIMIT 0,' at line 1
I don't think I've ever seen an IF() used as a table reference in a query, especially if the IRC source can alternate between different sources... I would change to...
select
irc.*,
if( p1.id = irc.itemid, p1.fld1, p2.fld1 ) as Fld1,
if( p1.id = irc.itemid, p1.fld2, p2.fld2 ) as Fld2,
if( p1.id = irc.itemid, p1.fld3, p2.fld3 ) as Fld3,
if( p1.id = irc.itemid, p1.fld4, p2.fld4 ) as Fld4
from
item_rel_coupon irc
join user_rel_coupon urc
on urc.userId = 7
and irc.couponId=urc.couponId
left join prod_product p1
on p1.id = irc.itemid
left join pub_accessrole p2
on p2.id = irc.itemid