ArrozConTé
ArrozConTé

Reputation: 51

SQL SELECT various names by various id's

I have this 3 tables:

main table:

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

province table:

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

town table:

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

Obviously the main table has foreign relationships with the province table and the town table.

The result I want is this one:

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

I want to show the main table but with the names instead of the ID's.

I looked up other questions and I tried this one:

    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.

And this other one:

 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;

Also I would like that the query could be easy to use in the way that If I have the same problem, but instead of the main table and 2 tables, I have main table and 4 tables I could also use it just adding more "INNER JOINS" for example?

I'm trying to do this but I couldn't get it right, I get this Sql wrong every time and I don't find the correct way to do this, any ideas?

I'm using PDO and I show this on a web page.

"AND Id=1" --> this will be used like this --> "AND Id=:filterID".

I hope I explained myself right!

If you have any doubts just ask, I'll answer as fast as I can!

Thanks in advance!

---------------------------------------------------------------EDIT--------------------------------------------------------------

I already tryed the 3 answers I got and it doesn't work, I don't know why, I will add images to back up so you see.

This one works with no errors but I get no info back

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

This one the same and no info back, but anyways I get extra columns from the other tables that I don't need there.

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

I also tryed this one but I can't get it to work always shows me an error

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

I can't post more than 2 photos because I don't have enough reputation but I paste the error here:

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

And in case you ask or doubt, I did put the info in the tables, 1 row each so there is something in, but for some reason it doesn't gives me back that info.

-------------------------------------------------------LAST EDIT-------------------------------------------------------------

The 3 codes worked for me after trying them with a few edits, here are the 3 codes so people who might read this find this helpful.

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;

Thanks a lot to the people who have helped me!

Upvotes: 0

Views: 84

Answers (3)

johannespartin
johannespartin

Reputation: 501

I think you are just using a wrong SQL syntax on the second guess.

Try this instead:

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;

The first guess is by the way old syntax and should not be used.

Edit: adapted to MariaDB Syntax

Upvotes: 1

Zohar Peled
Zohar Peled

Reputation: 82474

Well, you are close, it's just that your join condition is wrong:

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;

Upvotes: 2

Milan rathod
Milan rathod

Reputation: 1

this is work for me ,

Please feild name first ,if its not proper then first fix it then execute ,

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;

Upvotes: 0

Related Questions