开发者

Left join/foreign key question for mysql in php

开发者 https://www.devze.com 2023-03-26 06:44 出处:网络
I have tables queue and requests. queue\'s reqID is a foreign key to requests\' reqID. both tables have vanID

I have tables queue and requests. queue's reqID is a foreign key to requests' reqID. both tables have vanID

I am trying to get all the stuff in queue, as well as all the information for each request that queue references. This is my query.

$sql = mysql_query("SELECT queue.*, requests.* 
                      FROM queue 
                     WHERE queue.vanID = '$vanID' 
                 LEFT JOIN requests ON queue.reqID = requests.reqID 
    开发者_C百科              ORDER BY rank ASC") or die(mysql_error());

This the error I'm getting.

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 requests ON queue.reqID = requests.reqID ORDER by rank ASC' at line 1

I can't seem to figure it out, any help?


$sql = mysql_query("SELECT queue.*, requests.* FROM queue LEFT JOIN requests ON queue.reqID = requests.reqID WHERE queue.vanID = '$vanID' ORDER by rank ASC") or die(mysql_error());

You were close. The WHERE clause should go after the LEFT JOIN.


The joins are part of the from section of a query, and, as such, must come before the where section :

SELECT queue.*, requests.* 
FROM queue 
    LEFT JOIN requests ON queue.reqID = requests.reqID 
WHERE queue.vanID = '$vanID' 
ORDER by rank ASC
0

精彩评论

暂无评论...
验证码 换一张
取 消