using where and inner join in mysql

前端 未结 4 1738
攒了一身酷
攒了一身酷 2020-12-08 13:56

I have three tables.

locations

ID   | NAME | TYPE |
1    | add1 | stat |
2    | add2 | coun | 
3    | add3 | coun |
4    | add4 | co         


        
相关标签:
4条回答
  • 2020-12-08 14:32

    You can use as many joins as you want, however, the more you use the more it will impact performance

    0 讨论(0)
  • 2020-12-08 14:33
        SELECT `locations`.`name`
          FROM `locations`
    INNER JOIN `school_locations`
            ON `locations`.`id` = `school_locations`.`location_id`
    INNER JOIN `schools`
            ON `school_locations`.`school_id` = `schools_id`
         WHERE `type` = 'coun';
    

    the WHERE clause has to be at the end of the statement

    0 讨论(0)
  • 2020-12-08 14:52

    Try this:

    SELECT Locations.Name, Schools.Name
    FROM Locations
    INNER JOIN School_Locations ON School_Locations.Locations_Id = Locations.Id
    INNER JOIN Schools ON School.Id = Schools_Locations.School_Id
    WHERE Locations.Type = "coun"
    

    You can join Locations to School_Locations and then School_Locations to School. This forms a set of all related Locations and Schools, which you can then widdle down using the WHERE clause to those whose Location is of type "coun."

    0 讨论(0)
  • 2020-12-08 14:54

    Try this :

    SELECT
        (
          SELECT
              `NAME`
          FROM
              locations
          WHERE
              ID = school_locations.LOCATION_ID
        ) as `NAME`
    FROM
         school_locations
    WHERE
         (
          SELECT
              `TYPE`
          FROM
              locations
          WHERE
              ID = school_locations.LOCATION_ID
         ) = 'coun';
    
    0 讨论(0)
提交回复
热议问题