0

three tables - articles, galleries, images.

articles has a column gallery (integer - id of included gallery)

galleries has a column images (csv string - ids of included images)

images has id and src columns.

Now I need to get src of images -> included in a gallery -> included in an article.

I firstly select gallery id from the table articles

Then - using the given gallery id - select images ids from the table galleries

Then using the given images-ids - select the sources of images from the table images.

This scenario is tedious, requires a lot lot of coding.

Is there any shorter, easier way?

I cannot write img sources or img ids directly inside articles because need to get changes inside articles automatically - when a change inside galleries or images is done.

Any help?

qadenza
  • 9,025
  • 18
  • 73
  • 126
  • See normalisation – Strawberry Jul 14 '19 at 05:19
  • @Strawberry, I see the problem with `csv` inside a column, but replacign the `csv` with a separate row for each element - `doesn't solve` the problem with three tables as described in my post. So your link is `related` or `helping` but the question is not duplicated. – qadenza Jul 14 '19 at 05:33
  • The point really is that MySQL is a relational database management system. If you don't want to use relational data, that's fine, but then don't use (or why bother with) a relational database management system. – Strawberry Jul 14 '19 at 06:35

1 Answers1

0

use JOIN

$sql = 'SELECT a.id, a.column_name,c.sources FROM articles a,galleries b,images c WHERE a.id = b.id and b.id = c.id';

  if($result = mysqli_query($conn, $sql)) {
  // your desire calculation

  } else {
        echo "No records matching your query were found.";
  }
Rajkumar Sharma
  • 554
  • 1
  • 4
  • 9