MySQL - Doing COUNT (*) for the current row

Here's my request SELECT

:

SELECT col1, col3, col5, col8
FROM Table1

      

In my query, SELECT

I want to do COUNT (*) exclusively on the current row .

I want something like this, but have no idea how I can do this:

SELECT col1, col3, 
(
SELECT COUNT(*) 
FROM table1 WHERE col3 = col3 value for current row
), 
col5, col8
FROM Table1

      

What is the correct way to do COUNT (*) on the current row of the query result set SELECT

?

+3


source to share


3 answers


try this:

set @num := 0;
SELECT @num := @num+1 , col1, col3, col5, col8 FROM Table1

      



or in another way:

SET @num := ( SELECT COUNT( * ) FROM Table1) ;
SELECT @num := @num -1 , col1, col3, col5, col8
FROM Table1

      

+2


source


SELECT col1, col3, 
(
    SELECT COUNT(*) 
    FROM table1 WHERE id = A.Id
) Count, 
col5, col8
FROM Table1 A

      



+1


source


Found.

If I write my request like this:

SELECT col1, col3, 
COUNT(*) AS count,
col5, col8
FROM Table1

      

then i get the COUNT of all items in table1 for all rows.

I really need COUNT for all items for selectable rows.

Like mine, col3 value appears more than once. I really want to count all elements for each col3.

So the proposal GROUP BY

is the solution.

SELECT col1, col3, 
COUNT(*) AS Count, 
col5, col8
FROM Table1 
GROUP BY col3

      

Sorry, it seems to me that I was not able to explain my problem correctly, so it confused a lot of people. A very bad question. I'm sorry.

+1


source







All Articles