Rafael Ribeiro
Rafael Ribeiro

Reputation: 13

WRONG MYSQL QUERY

I have a mysql table where "meeting_id" is a colum. I need to show only results that is related to this meeting ID on table POLLS

The actual query is:

$stmt = $pdo->prepare('SELECT p.*, GROUP_CONCAT(pa.title ORDER BY pa.id) AS answers 
                        FROM polls p 
                        LEFT JOIN poll_answers pa ON pa.poll_id = p.id 
                        GROUP BY p.id');

I have tried without success:

$stmt = $pdo->prepare('SELECT p.*, GROUP_CONCAT(pa.title WHERE meeting_id = '.$meeting_id.' ORDER BY pa.id) AS answers 
                        FROM polls p 
                        LEFT JOIN poll_answers pa ON pa.poll_id = p.id 
                        GROUP BY p.id');

Thanks Guys

Upvotes: 1

Views: 21

Answers (1)

Barmar
Barmar

Reputation: 780818

The WHERE clause doesn't go inside GROUP_CONCAT(), it goes after FROM and JOIN.

You should also use parameters, not variable concatenation.

$stmt = $pdo->prepare('SELECT p.*, GROUP_CONCAT(pa.title ORDER BY pa.id) AS answers 
                        FROM polls p 
                        LEFT JOIN poll_answers pa ON pa.poll_id = p.id 
                        WHERE p.meeting_id = :meeting_id
                        GROUP BY p.id');
$stmt->bindParam(':meeting_id', $meeting_id);

Upvotes: 1

Related Questions