MYSQL INNER JOIN with possible empty fields

前端 未结 2 1516
灰色年华
灰色年华 2021-02-14 09:51

Ok, I have a problem with joining 2 tables (with INNER JOIN). First table containts categories list (ecc. for photo albums, or blog posts) while the second table contains \"the

相关标签:
2条回答
  • 2021-02-14 10:07

    To retain the rows from galeries with no matching ID in photos, you will need to join photos on galeries with LEFT JOIN instead of INNER JOIN:

    SELECT galeries_id, galeries_title, 
           photos.photos_id, photos.photos_gal_id, photos.photos_link 
    FROM galeries 
    LEFT JOIN photos 
    ON galeries.galeries_id=photos.photos_gal_id 
    GROUP BY photos_gal_id
    

     

    This will give you:

    galeries_id      galeries_title       photos_id       photos_link
         1               blabla              3            test.jpg
         2               bla bla2            NULL         NULL                
         3               etata               5            test.jpg
    

     

    And if you wish to replace NULL with an empty string, you can use:

    SELECT
      IFNULL(photos.photos_id, ''),
      IFNULL(photos.photos_link, '')
    
    0 讨论(0)
  • 2021-02-14 10:16

    Wouldn't it just be a

    LEFT JOIN photos
    

    Because you want the records reguardless of wether or not they are filled?

    0 讨论(0)
提交回复
热议问题