简体   繁体   English

MySQL多个内部联接

[英]mySQL multiple inner joins

I have two tables I'm pulling information from. 我有两个要从中获取信息的表。

Lets say table1 has following columns (id, title, category, sub_category, sub_sub_category) 可以说table1具有以下列(id,标题,类别,sub_category,sub_sub_category)

Lets say table2 has following columns (category_id, category_name) 可以说table2具有以下列(category_id,category_name)

I have a select statement that so far looks as follows: 到目前为止,我有一条select语句如下:

SELECT
  table1.id,
  table1.title,
  table2.category_name as Cat1,
  table2.category_name as Cat2,
  table2.category_name as Cat3
FROM
  table1,
  table2
INNER JOIN table2 as c1 ON c1.category_id = table1.category
INNER JOIN table2 as c2 ON c2.category_id = table1.sub_category
INNER JOIN table2 as c3 ON c3.category_id = table1.sub_sub_category
WHERE
  table1.id = ?

This gives me an error about table1.category being an unknown column 这给我关于table1.category是未知列的错误

I have also tried 我也尝试过

SELECT
  table1.id,
  table1.title,
  table2.category_name as Cat1,
  table2.category_name as Cat2,
  table2.category_name as Cat3
FROM
  table1,
  table2
WHERE table1.id = ?
AND   table1.category = table2.category_id
AND   table1.sub_category = table2.category_id
AND   table1.sub_sub_category = table2.category_id

The last example at least gives me column output I'm looking for which would be 最后一个示例至少为我提供了我正在寻找的列输出

(table1.id, table1.title, table1.category name, table1.sub_category name...) (table1.id,table1.title,table1.category名称,table1.sub_category名称...)

So showing the category name from table 2 instead of the ID's. 因此,显示表2中的类别名称而不是ID。 I am an amateur coder and haven't had to use inner joins before but maybe that is what I need to do. 我是一名业余编码员,以前没有使用内部联接的方法,但是也许这是我需要做的。 I just can't figure out how to get it to output the data I need. 我只是不知道如何获取它来输出所需的数据。

Thank you in advance for your time and consideration. 预先感谢您的时间和考虑。

Your problem is that you have a comma in the from clause. 您的问题是from子句中有逗号。 Simple rule: never use commas in the from clause. 简单的规则:请勿在from子句中使用逗号。 Always use explicit join syntax. 始终使用显式join语法。

Then, you also have table2 mentioned an extra time, and your select is pulling columns from the wrong instance of table2 . 然后,您还提到了table2额外的时间,并且您的select是从错误的table2实例中提取列。

The fixed up query looks like: 固定查询如下所示:

SELECT t1.id, t1.title,
       c1.category_name as Cat1, c2.category_name as Cat2,
       c3.category_name as Cat3
FROM table1 t1 INNER JOIN
     table2 c1
     ON c1.category_id = t1.category INNER JOIN
     table2 c2
     ON c2.category_id = t1.sub_category INNER JOIN
     table2 c3
     ON c3.category_id = t1.sub_sub_category
WHERE t1.id = ?;

Ups, you have a couple of problems here. Ups,您在这里遇到了两个问题。 First table2 can have only one column called category_name so there is no reason to select it three times like you do, result of that would be that you have three absolutely same column with different name. 第一个表2只能有一个名为category_name的列,因此没有理由像您一样选择三遍,结果是您将拥有三个绝对相同的列,但名称不同。

Second the syntax is completely wrong. 其次,语法是完全错误的。 Syntax for INNER JOIN is INNER JOIN的语法是

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

also you need to have column in both table usually foreign key and that column will be use to match data. 同样,您还需要在两个表中都具有通常是外键的列,并且该列将用于匹配数据。 I guess in your case it should be category_id from second table and in first table that's column you named category (it, should be renamed at category_id it's more convenient)... 我想在您的情况下,它应该是第二个表中的category_id,而在第一个表中您将该列命名为category(它应该在category_id中重命名,这样更方便)...

So if understand right you want to select id, title from first table and category_name from second you could do that like this: 因此,如果您了解正确,则想从第一张表中选择id,title和第二张表中的category_name,您可以像这样:

SELECT table1.id, table1.title, table2.category_name
FROM table1
INNER JOIN table2
ON table1.category_id = table2.category_id;

And friendly advice to you is to find some online sources and learn a little bit more about this before you continue with what ever you do... 给您的友善建议是在继续进行之前,找到一些在线资源并了解更多有关此的信息。

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

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