0

I'm trying to get two sets of row count.

One with the total number of rows in a specific table, and a second count on the number of rows returned based on a WHERE statement - from the same table.

It's a basic thing I guess. The text says "We have ### number of partners, and ## of them are displayed on your site". I also need to retrieve data, and not just the row-counts.

Do I have to make multiple SQL-queries to the database? or is there a more efficient way to collect this info from on SQL-query?

My queries looks like this:

$visable_dealers = $pdo->query('SELECT COUNT(1) FROM dealers WHERE visable = 1')->fetchColumn();
$all_dealers = $pdo->query('SELECT * FROM dealers')->fetchAll();

I could do a foreach-loop, and then add +1 to a variable every time $val['visable'] is equal to 1. But I would get that number too late. I need it before the loop.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
ThomasK
  • 2,210
  • 3
  • 26
  • 35
  • 1
    *I could do a `foreach`-loop, and then add `+1`* No you shouldn't do that way! `:(` How are you displaying it on the screen? The result set should have the number of rows in the particular page. – Praveen Kumar Purushothaman Apr 04 '18 at 12:22
  • @TomUdding a little differences between 2 questions. This one has 2 queries and that one has only 1 query. – terry.qiao Apr 04 '18 at 12:30
  • I think it can not be done in one query, as one is trying to fetch all records (which is a list), while another one is just fetching a count (which is only one record). Of course you can not merge these 2 result sets into one set. – terry.qiao Apr 04 '18 at 12:36
  • I think we both came to the same conclusion. But I just needed to check if this had been dealt with in another manner... One never knows... – ThomasK Apr 04 '18 at 18:30

0 Answers0