0

I am trying to select the COUNT with three tables with one single query (with WHERE conditions).

Here is my code which doesn't work correctly.

SELECT t1.count(id) AS car_model_count,t2.count(id) AS list_item_count,t3.count(id) 
    FROM `car_model` AS t1
    INNER JOIN `list_item` AS t2
    INNER JOIN `part_item` AS t3
    WHERE t1.user_id=3;
Balan K
  • 47
  • 1
  • 8

1 Answers1

1

Possible by using Sub-Query OR UNION is possible to get the COUNT from multiple table. Try this query :

SELECT 
    (SELECT count(*) FROM `car_model` WHERE user_id=3 ) AS car_model_count,
    (SELECT COUNT(*) FROM `list_item` WHERE user_id=3) AS list_item_count,
    (SELECT count(*) FROM `part_item` WHERE user_id=3) AS part_item_count;
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46