0

Basically I am trying to fetch details of pages stored in pages table in database.

I want to fetch all records from page table and the records having user_id 16 from pages_commission table.

The query I tried

SELECT pages.*, IFNULL(pc.commission, 0) AS commission FROM `pages` LEFT JOIN `pages_commission` AS pc ON pages.page_id=pc.page_id WHERE pc.user_id=16

But I am gettings zero rows.

Table structure:

  1. pages table

pages tale

  1. pages_commission table

pages_commission

Database: MySql

Shadow
  • 33,525
  • 10
  • 51
  • 64
Mayur Fartade
  • 317
  • 3
  • 18

1 Answers1

1

The WHERE is turning the join into an inner join. Move the constant condition to the ON clause:

SELECT p.*, COALESCE(pc.commission, 0) AS commission
FROM `pages` p LEFT JOIN
     `pages_commission`pc
     ON p.page_id = pc.page_id AND pc.user_id = 16;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786