简体   繁体   English

使用JOIN对多个表进行SQL查询

[英]SQL query for multiple tables using JOIN

Need help with SQL query - my query at the moment works with two tables I want to add third table, but i have no idea how to do that, I tried many times but it gives error... 需要有关SQL查询的帮助-目前我的查询可用于两个表,我想添加第三个表,但是我不知道该怎么做,我尝试了很多次,但是给出了错误...

I will try to show all my code as more acurate: 我将尝试以更准确的方式显示所有代码:

SQL QUERY SQL查询

$sql = "SELECT main.nid, main.title FROM {node} AS main
                    LEFT JOIN {localizernode} AS lang ON main.nid = lang.nid 
                    WHERE main.type = 'drug' AND main.title LIKE '%s%%' AND lang.language = '%s' AND main.status = 1 
                    ORDER BY main.title ASC";

$result = db_query($sql);
while ($product = db_fetch_object($result)) {
// build up the list
$product_list .= '<li>'.$product->title.'</li>';
} 

this QUERY grabs all data from table NODE based on website language where type is drug. 该查询根据类型为毒品的网站语言从表NODE中获取所有数据。 And give me a list of all items in side that table, ordering by title 然后给我一张桌子旁边所有物品的清单,按标题排序

Table "node": 表“节点”:

nid | nid | vid | vid | type | 类型 title | 标题| status 状态

9 | 9 | 9 | 9 | drug | 毒品| Title 1 | 标题1 | 1 1

15 | 15 | 15 | 15 | drug | 毒品| Title 2 | 标题2 | 1 1


Table "content_type_drug" which I want to use/include in QUERY looks like: 我要在QUERY中使用/包含的表“ content_type_drug”如下所示:

vid | vid | nid | nid | value | 价值| image_title image_title

9 | 9 | 9 | 9 | Text value | 文字值| imagename.jpg imagename.jpg

15 | 15 | 15 | 15 | Text value5 | 文字值5 | imagename3.jpg imagename3.jpg

What I want it to grab from "content_type_drug" the following values: "value", "image_title" and display them in the above list: 我希望它从“ content_type_drug”中获取以下值:“ value”,“ image_title”并将它们显示在上面的列表中:

$result = db_query($sql);
    while ($product = db_fetch_object($result)) {
    // build up the list
    $product_list .= '<li>'.$product->title.$product->value.$product->image.'</li>';
    }

I am looking for help, and not for FULL solution, please give me any ideas. 我在寻求帮助,而不是寻求完整的解决方案,请给我任何想法。

What i understand that you need to join three tables. 我了解到您需要加入三个表。 So query will be 所以查询将是

$sql = "SELECT main.nid, main.title, ctd.value,ctd.image_title FROM {node} AS main
        LEFT JOIN {localizernode} AS lang ON main.nid = lang.nid 
        INNER JOIN {content_type_drug} ctd ON main.vid = ctd.vid 
        WHERE main.type = 'drug' AND main.title LIKE '%s%%' 
        AND lang.language = '%s' AND main.status = 1  ORDER BY main.title ASC";

I am assuming that vid and nid are foreign keys. 我假设vid和nid是外键。 You can replace this clause with your foreign keys 您可以使用外键替换此子句

To add a new table, you just need to add another join. 要添加新表,您只需要添加另一个联接。

LEFT JOIN {content_type_drug} AS drug ON drug.vid = main.vid 

So your full query will look like: 因此,您的完整查询将如下所示:

SELECT main.nid, main.title, drug.value, drug.image_title FROM {node} AS main
                LEFT JOIN {localizernode} AS lang ON main.nid = lang.nid 
                LEFT JOIN {content_type_drug} AS drug ON drug.vid = main.vid 
                WHERE main.type = 'drug' AND main.title LIKE '%s%%' AND lang.language = '%s' AND main.status = 1 
                ORDER BY main.title ASC

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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