What's wrong with this MySQL query?
Sorry because this is a noob question. I'm new with MySQL:
I wrote a query like this:
SELECT
u.userid, u.alias, g.company_name,
v.endtime - v.begintime AS duration,
u.status, u.service_starttime,
u.service_expiretime, v.begintime, u.email
FROM
company_users c, company_groups g INNER JOIN
user_info u INNER JOIN vfon_log v
ON (u.userid = v.hostid) ON (g.company_id = u.company_id)
This query returns a syntax error:
Query : SELECT u.userid, u.alias, g.company_name, v.endtime - v.begintime AS duration, u.status, u.service_starttime, u.service_ex...
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 'ON (g.company_id = u.company_id)
LIMIT 0, 100开发者_如何学编程0' at line 4
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
I've spent 30 minutes looking, but I can't figure out what's wrong.
Thank you so much for your helps
ON (g.company_id = u.company_id)
should be after INNER JOIN user_info u
So it becomes
SELECT
u.userid, u.alias, g.company_name,
v.endtime - v.begintime AS duration,
u.status, u.service_starttime,
u.service_expiretime, v.begintime, u.email
FROM
company_users c, company_groups g
INNER JOIN user_info u ON (g.company_id = u.company_id)
INNER JOIN vfon_log v ON (u.userid = v.hostid)
Your field is not vbegintime
but v.begintime
You put the ON
statements on the wrong place. The standard solution is to add it directly after the join:
SELECT *
FROM company_users c,
company_groups g INNER JOIN
user_info u ON (g.company_id = u.company_id)
INNER JOIN vfon_log v
ON (u.userid = v.hostid)
Or you can use brackets to get the correct ON
linked to the correct INNER JOIN
:
SELECT *
FROM company_users c,
company_groups g INNER JOIN
(user_info u INNER JOIN vfon_log v
ON (u.userid = v.hostid))
ON (g.company_id = u.company_id)
I think it should b
SELECT u.userid, u.alias, g.company_name, v.endtime - vbegintime AS duration,
u.status, u.service_starttime, u.service_expiretime, v.begintime, u.email
FROM company_users c, company_groups g
INNER JOIN
(user_info u INNER JOIN vfon_log v ON (u.userid = v.hostid))
ON g.company_id = u.company_id
精彩评论