SQL SELECT各种id的各种名称

时间:2017-03-29 10:29:50

标签: php mysql

我有这3张桌子:

主表:

  ID | OtherStuff1 | OtherStuff2 | IdProvince | IdTown 
-----+-------------+-------------+------------+--------
   1 |   Stuff1    |   Stuff2    |      1     |   1    

省表:

ID | ProvinceName 
---+--------------
 1 |  ProvName1   

城镇表:

ID |   TownName   
---+--------------
 1 |   TwName1  

显然,主表与省表和城镇表有外交关系。

我想要的结果就是这个:

ID | OtherStuff1 | OtherStuff2 | IdProvince |   IdTown   
---+-------------+-------------+------------+------------
 1 |   Stuff1    |   Stuff2    | ProvName1  |   TwName1  

我想显示主表但是使用名称而不是ID。

我查了解其他问题,我尝试了这个问题:

    SELECT *
        FROM main AS a 
        INNER JOIN province AS b
        INNER JOIN town AS c
        WHERE a.IdProvince = b.ProvinceName
        AND a.IdTown = c.TownName 

    AND Id=1; # this is added by me because I want to filter by id on the
              # main table, I don't want all the rows, just one filtered by ID.

另一个:

 SELECT *
       FROM main AS a, 
       INNER JOIN province AS b ON a.IdProvince = b.ProvinceName
       INNER JOIN town AS c ON a.IdTown = c.TownName
       WHERE Id=1;

此外,我希望查询可以很容易使用,如果我有相同的问题,但而不是主表和2表,我有主表和4表我也可以使用它只是添加例如更多的“INNER JOINS”?

我正在努力做到这一点,但我无法做到正确,我每次都得到这个Sql错误而且我找不到正确的方法来做这个,有什么想法吗?

我正在使用PDO,我在网页上显示。

“AND Id = 1” - >这将使用这样 - > “AND Id =:filterID”。

我希望我自己解释好了!

如果您有任何疑问,我会尽快回答!

提前致谢!

----------------------------------------------- - - - - - - - - 编辑 - - - - - - - - - - - - - - - - - -----------------------------

我已经尝试了我得到的3个答案但它不起作用,我不知道为什么,我会添加图像备份,所以你看。

这个没有错误,但我没有收到任何信息

SELECT a.Id, OtherStuff1, OtherStuff2, IdProvince, IdTown
 FROM main AS a 
 INNER JOIN province AS b 
 INNER JOIN town AS c 
   WHERE a.IdProvince = b.ProvinceName 
   AND a.IdTown = c.TownName 
AND a.Id=1

This is the result

这个相同而且没有回复信息,但无论如何我从其他表中获得额外的列,我不需要那些。

SELECT * 
 FROM main AS a ,province AS b , town AS c 
  WHERE a.IdProvince = b.ProvinceName 
  AND a.IdTown = c.TownName 
AND a.Id=1

This is the second result

我也试过这个,但我无法让它工作总是给我一个错误

SELECT *
   FROM 
      (main AS a 
      INNER JOIN province AS b ON a.IdProvince = b.ProvinceName) AS d
      INNER JOIN town AS c ON d.IdTown = c.TownName
   WHERE 
      Id=1

我无法发布超过2张照片,因为我没有足够的声誉,但我在此处粘贴错误:

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'd
      INNER JOIN town AS c ON d.IdTown = c.TownName
   WHERE
      Id=1 LI' at line 4

如果您提出疑问或疑问,我确实将信息放在表格中,每行1行,因此有一些内容,但由于某种原因,它不会让我回复该信息。

----------------------------------------------- --------最后编辑---------------------------------------- ---------------------

通过一些编辑尝试后,3个代码对我有用,这里有3个代码,所以读过这个代码的人会觉得这很有帮助。

SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName
    FROM main AS a 
    INNER JOIN province AS b ON a.IdProvince = b.Id
    INNER JOIN town AS c ON a.IdTown = c.Id 
    WHERE a.Id=1;
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName 
    FROM main AS a 
    INNER JOIN (province AS b, town AS c) 
    ON (a.IdProvince = b.Id AND a.IdTown = c.Id) 
    WHERE a.Id=1;
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName 
    FROM main AS a ,province AS b , town AS c 
    WHERE a.IdProvince = b.Id
    AND a.IdTown = c.Id 
    AND a.Id=1;

非常感谢帮助过我的人们!

3 个答案:

答案 0 :(得分:2)

嗯,你很亲密,只是你的加入条件错了:

SELECT  main.ID, OtherStuff1, OtherStuff2, ProvinceName, TownName
    FROM main AS a 
    INNER JOIN province AS b ON a.IdProvince = b.Id
    INNER JOIN town AS c ON a.IdTown = c.Id 
    WHERE main.Id=1;

答案 1 :(得分:1)

我认为你只是在第二次猜测时使用了错误的SQL语法。

请改为尝试:

SELECT *
   FROM 
      main AS a INNER JOIN (province AS b, town AS c)
      ON (a.IdProvince = b.Id AND a.IdTown = c.Id)
      WHERE 
      a.Id=1;

第一个猜测是旧语法的方式,不应该使用。

编辑:改编为MariaDB语法

答案 2 :(得分:0)

这对我有用,

请先命名,如果不正确则首先修复然后执行,

SELECT * 
FROM main AS a ,province AS b , town AS c 
WHERE a.IdProvince = b.id
AND a.IdTown = c.id 
AND a.Id=1;