A
A
Alexander2020-05-25 01:04:28
PHP
Alexander, 2020-05-25 01:04:28

Duplicate string in Mysql PHP query?

Hello, when trying to make a mysql query, the rows are duplicated in the output, the query itself:

$q = "
  SELECT wall.id, wall.owner_id, wall.user_id, wall.text, wall.active, wall.date, wall_photos.photo, wall_audios.audio
  FROM wall 
  LEFT JOIN wall_photos 
  ON wall.id = wall_photos.wall 
  AND wall_photos.active = 1
  LEFT JOIN wall_audios 
  ON wall.id = wall_audios.wall 
  AND wall_audios.active = 1
  WHERE owner_id = ? 
  AND wall.active = 1 
  ORDER BY wall.date DESC";

  $wall = $db->rawQuery($q, [$owner_id]);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Gordinskiy, 2020-05-25
@DmitriyGordinskiy

As an option to scoff around this grouping request:

SELECT 
  wall.id, 
  wall.owner_id, 
  wall.user_id, 
  wall.text, 
  wall.active, 
  wall.date, 
  GROUP_CONCAT(DISTINCT(wall_photos.photo)), 
  GROUP_CONCAT(DISTINCT(wall_audios.audio))
FROM wall 
  LEFT JOIN wall_photos 
  ON wall.id = wall_photos.wall 
  AND wall_photos.active = 1
  LEFT JOIN wall_audios 
  ON wall.id = wall_audios.wall 
  AND wall_audios.active = 1
  WHERE owner_id = ? 
  AND wall.active = 1 
  GROUP BY wall.id
  ORDER BY wall.date DESC

F
FanatPHP, 2020-05-25
@FanatPHP

You already answered 5 hours ago

If you want to get all the photos and videos on the wall - make three requests: to wall, wall_photos and wall_audios separately

What is not clear here?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question