mysql max function only returns 1 row

DCR

I have the following query to my db:

$examCnt = query("select * from apExams");

when I print that out I get:

Array
(
[0] => Array
    (
        [date] => 2023-08-24
        [exam] => 1
        [question] => 1
        [active] => 0
    )

[1] => Array
    (
        [date] => 2023-08-24
        [exam] => 1
        [question] => 2
        [active] => 0
    )

[2] => Array
    (
        [date] => 2023-08-25
        [exam] => 3
        [question] => 1
        [active] => 0
    )

[3] => Array
    (
        [date] => 2023-08-25
        [exam] => 3
        [question] => 2
        [active] => 0
    )

)

but when I run the following query:

 $examCnt = query("select *, max(exam) as cnt from apExams");

I get:

Array
(
    [0] => Array
        (
            [date] => 2023-08-24
            [exam] => 1
            [question] => 1
            [active] => 0
            [cnt] => 3
        )

)

Why don't I get back all 4 records with cnt = 3?

Barmar

When you don't use GROUP BY, any use of an aggregate function causes the entire table to be aggregated into one row in the result set.

To get what you want, you should use MAX(cnt) in a subquery.

SELECT *, (SELECT MAX(exam) FROM apExams) AS cnt 
FROM apExams

or

SELECT *
FROM apExams
CROSS JOIN (
    SELECT MAX(exam) AS cnt
    FROM apExams
) AS x

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MySQL LEFT JOIN only 1 row depending on MAX() value

Missing row when using mysql max function

Only in my server returns "Subquery returns more than 1 row"

MySQL - Subquery returns more than 1 row

Mysql select row of max id is returns wrong data

Call a stored function in mysql,But get an error:Subquery returns more than 1 row

MySQL returns only first line when querying for MAX() and all names

Function returns only 1 array result

MySQL WHERE IN Returns only 1 record

Max() function not returns the max value

R: Function only Produces 1 Row of Data

Get 1 row only from MySQL in CodeIgniter

How to select only 1 row with column max value in SQL?

Using the LIMIT function to display the 1st, 2nd and 3rd row, with the MAX customer rating using MySQL

MySQL - MAX() returns null

Using avg() function in SQL query returns only one row?

mysql query on column "serialNo" only returns the first row

MYSQL LEFT JOIN only returns one row from joined table

SQL Declare Query only returns 1 row result

MAX function returning a row

vba legacy function to return row count returns 1 instead

MYSQL Error - Subquery returns more than 1 row

MySQL: Error code : #1242 - Subquery returns more than 1 row

Error Code: 1242 Subquery returns more than 1 row mysql

MySQL said: Documentation- Subquery returns more than 1 row

Subquery returns more than 1 row - Multiple selects MySQL

Subquery returns more than 1 row in MySQL procedure

MySQL - Subquery Returns more than 1 row - subtraction issue

Strange MySQL error #1242 - Subquery returns more than 1 row