这个MySQL查询有什么问题?

时间:2021-11-24 21:45:28

Sorry because this is a noob question. I'm new with MySQL:

不好意思,这是一个不能回答的问题。我对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, 1000' 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.

我花了30分钟的时间寻找,但是我不知道是怎么回事。

Thank you so much for your helps

非常感谢你的帮助

4 个解决方案

#1


2  

ON (g.company_id = u.company_id)

should be after INNER JOIN user_info u

应该在内部连接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) 

#2


1  

Your field is not vbegintime but v.begintime

你的字段不是起始时间而是起始时间

#3


1  

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)

#4


0  

I think it should b

我想应该是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

#1


2  

ON (g.company_id = u.company_id)

should be after INNER JOIN user_info u

应该在内部连接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) 

#2


1  

Your field is not vbegintime but v.begintime

你的字段不是起始时间而是起始时间

#3


1  

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)

#4


0  

I think it should b

我想应该是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