这个MySQL查询有什么问题?

时间:2010-07-12 08:21:03

标签: sql mysql tsql

很抱歉,因为这是一个菜鸟问题。我是MySQL的新手:

我写了一个这样的查询:

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)

此查询返回语法错误:

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, 1000' at line 4
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

我花了30分钟看,但我无法弄清楚出了什么问题。

非常感谢您的帮助

4 个答案:

答案 0 :(得分:2)

ON (g.company_id = u.company_id)

应该在INNER JOIN user_info u之后

所以它变成

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) 

答案 1 :(得分:1)

您将ON语句放在错误的位置。标准解决方案是在加入后直接添加它:

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)

或者您可以使用括号将正确的ON链接到正确的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)

答案 2 :(得分:1)

您的字段不是vbegintime,而是v.begintime

答案 3 :(得分:0)

我认为应该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