简体   繁体   中英

mysql query problem inner join

Can I do this query, if not how can I change it to work

SELECT
    clientinfo.clientname, clientinfo.clientcode, clientinfo.serverid,
    serverslist.servername
FROM clientinfo 
where clientinfo.clientjurisdiction='$groupid' && clientinfo.clientcode REGEXP '$expression' 

INNER JOIN 
serverslist ON clientinfo.serverid=serverslist.serverid 
ORDER BY clientinfo.clientcode 

You have to move the INNER JOIN into the FROM clause like this:

SELECT c.clientname, c.clientcode, c.serverid, s.servername 
FROM clientinfo c INNER JOIN serverslist s ON c.serverid=s.serverid
WHERE c.clientjurisdiction='$groupid' && c.clientcode REGEXP '$expression' 
ORDER BY c.clientcode 

The order should be

SELECT ...
FROM ... INNER JOIN ... ON ...
WHERE ...
ORDER BY ...
SELECT
clientinfo.clientname, clientinfo.clientcode, clientinfo.serverid,
serverslist.servername
FROM clientinfo, serverslist 
where clientinfo.clientjurisdiction='$groupid' AND clientinfo.clientcode REGEXP    '$expression' AND 
serverslist ON clientinfo.serverid=serverslist.serverid 
ORDER BY clientinfo.clientcode 

The join clauses should be placed between the from and where clauses.

Also, you can use a table alias to make your query easier to read. eg.

SELECT
    c.clientname, c.clientcode, c.serverid, s.servername
FROM clientinfo c
    INNER JOIN serverslist s ON c.serverid = s.serverid 
WHERE c.clientjurisdiction = '$groupid' AND c.clientcode REGEXP '$expression' 
ORDER BY c.clientcode 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM