1

I have 2 MYSQL tables first table name Users and second table name images enter image description here

I want to margin 2 tables in one by FOREIGN KEY id but some users doesn't have imageurl_id how can i display all of them like this . and if there are no images i got empty image list

enter image description here

i tried this but it not working

 static async get() {
    return new Promise((resolve) => {
      db.execute("SELECT * FROM users,images where users.id = image.imageurl_id ", [], (error, result) => {
        if (!error) resolve(result);
      });
    });
  }
husam
  • 33
  • 1
  • 6
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Ken White Oct 14 '22 at 00:29
  • https://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join/6188334#6188334 – FanoFN Oct 14 '22 at 00:31
  • 2
    _"but some users doesn't have imageurl_id how can i display all of them"_ .. you're looking for a `LEFT JOIN`. – FanoFN Oct 14 '22 at 00:32
  • Also, don't use comma delimited joins from 90s. Use `join` syntax and the `on` operator for how the data relates. `SELECT * FROM users as u left join images as i on u.id = i.imageurl_id` – user3783243 Oct 14 '22 at 00:43
  • i knew the left join but when i use it i got results depends on how many image by id i have ,,,, redundant results depends on images – husam Oct 14 '22 at 19:45
  • What do you mean? What is wrong with the result if you run this query `SELECT * FROM users LEFT JOIN images where users.id = image.imageurl_id` ? – FanoFN Oct 18 '22 at 00:53

0 Answers0