Can I do this in one Mysql query? - php

I have a table with two columns:
column A column B
1 2
1 2
2 1
I want to return total of ones = 3 total of twos = 3
The best I can come up with is two queries like so:
SELECT sum(CASE WHEN columnA =1 THEN 1 ELSE 0 END )
+ sum(CASE WHEN columnB =1 THEN 1 ELSE 0 END )
SELECT sum(CASE WHEN columnA =2 THEN 1 ELSE 0 END )
+ sum(CASE WHEN columnB =2 THEN 1 ELSE 0 END )
Can this be done in one query?
Thanks

You didn't specify if you want to do this as 2 rows or as 2 values in a row.
Two rows are somewhat obvious (just union together all the values from each columns, and count(1) group by value against the result of the union; so I'll assume you want to do one row.
If you only have 1s or 2s, it's simple:
SELECT SUM(A+B-2) 'twos', SUM(4-A-B) 'ones' FROM myTable

SELECT SUM(IF(columnA=1, 1, 0) + IF(columnB=1, 1, 0)) as ones,
SUM(IF(columnA=2, 1, 0) + IF(columnB=2, 1, 0)) as twos
FROM myTable;
C.

To get everything in one query, I would try something like this.
SELECT Result.Val, COUNT(Result.Val) AS Count
FROM (
SELECT ColumnA AS Val
FROM TableName
UNION
SELECT ColumnB AS Val
FROM TableName
) AS Result
GROUP BY Result.Val

In general, you would count things like so:
SELECT columnA, COUNT(*) FROM myTable
GROUP BY columnA
to get the count of all different values in columnA.

SELECT COUNT(*) FROM table WHERE columnA=1 or columnB=1

Related

Mysql : last five records if having same value

I searched a lot and tried many queries but not getting satisfied answer. So like to ask.
I am looking for last 5 records from mysql table if having same value otherwise not.
Something like if col_n is having same value x from last 5 records then count otherwise not. But I am not able to figure out how to write query for this ?
SELECT count(col_n)
from track if(last five col_n = 'ok')
WHERE col_a = 'value1' AND col_b = 'value2'
enter mysql table records
Please try this:
SELECT p.*
FROM
( SELECT *
FROM demo ORDER by id DESC
LIMIT 5
) AS p
JOIN
( SELECT COUNT(*) AS cnt
FROM
( SELECT 1
FROM demo
LIMIT 5
) AS tmp
) AS c
ON c.cnt = 5 WHERE p.name='x'

Getting Count with different conditions in Select Query

The scenario is I have a column named "States" in a table, States can be 0,1,2,3 or 4. What I want to do is get a count of each state using WHERE State = in a single query.
The main purpose is I want to show the count of records (identified by their state). Like this, 20 records have State 0 etc.
Is this possible? If yes, then how can I achieve this?
Edit: I know about Count. Here's what I have tried:
SELECT State, Date_Created, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=1) as State_One, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=2) as State_Two, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=0) as State_Zero, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=4) as State_Four FROM [ECOS].[eco].[tb_projects_details] WHERE Date_Created < dateadd(week,-3,getdate());
If I understand it correctly, you want to group and count:
SELECT mt.States, COUNT(*) total
FROM my_table mt
GROUP BY mt.States
To generate a list of values and their counts you can do this:
SELECT State, COUNT(*) AS C
FROM mytable
GROUP BY State
To generate one row that contains value counts as columns you can do this:
SELECT
COUNT(CASE State WHEN 0 THEN 1 ELSE NULL END) AS State_0_Count,
COUNT(CASE State WHEN 1 THEN 1 ELSE NULL END) AS State_1_Count,
COUNT(CASE State WHEN 2 THEN 1 ELSE NULL END) AS State_2_Count,
COUNT(CASE State WHEN 3 THEN 1 ELSE NULL END) AS State_3_Count,
COUNT(CASE State WHEN 4 THEN 1 ELSE NULL END) AS State_4_Count
FROM [...]
WHERE [...]
It will be very easy if you use group by clause after where condition. Then you will get the number of each state very easily.
SELECT state, count(*)
FROM table_name
GROUP BY state
Next time read a sql for beginers book first.
Select state, count(*)
from table
group by state

Three columns - get MAX with SQL

I have table:
Rating:
id | one | two | three
1 | 12 | 3 | 7
2 | 11 | 30 | 3
3 | 8 | 14 | 4
How can i get with SQL MAX values from these fields (one, two, three)? For this example this is 30.
In MySQL you can use the GREATEST Function:
SELECT MAX(GREATEST(one, two, three))
FROM T;
Example on SQL Fiddle
SELECT MAX(field) FROM (
SELECT one AS field FROM table
UNION
SELECT two AS field FROM table
UNION
SELECT three AS field FROM table
) AS t
select GREATEST(max(one), max(two), max(three)) as maximum
from table;
You can unpivot the data similar to this:
select max(value)
from
(
select id, 'one' col, one value
from yourtable
union all
select id, 'two' col, two value
from yourtable
union all
select id, 'three' col, three value
from yourtable
) src
See SQL Fiddle with Demo.
Or you can use something like this:
SELECT max(data)
FROM
(
SELECT
CASE s.col
WHEN 'one' THEN one
WHEN 'two' THEN two
WHEN 'three' THEN three
END AS DATA
FROM yourtable t
CROSS JOIN
(
SELECT 'one' AS col
UNION ALL SELECT 'two'
UNION ALL SELECT 'three'
) s
) s
See SQL Fiddle with Demo
Try this query
SELECT tempTable.id, tempTable.max(val)
FROM (SELECT id, max(one) AS val
FROM tbl
UNION
SELECT id, max(two) AS val
FROM tbl
UNION
SELECT id, max(three) AS val
FROM tbl
) AS tempTable;
select max( if( one > two, if( one > three, one, three ), if( two > three, two, three ) )
from Rating
You can use case statement
SELECT
CASE
WHEN one >= two AND one >= three THEN one
WHEN two >= one AND two >= three THEN two
WHEN three >= one AND three >= two THEN three
ELSE one
END AS MaxVal

Select Row by Column1 Value to Select Column2 of same Row

I have a table like this:
id count
23432 0
34242 1
12345 5
32235 20
45645 3
How can i select a ROW column(count) value 20 by the column(id) value 32235?
I think this should be pretty straight-forward.
SELECT `count`
FROM tablename
WHERE id = 32235
SQLFiddle Demo
Use this:
SELECT * FROM table_name WHERE count = 20 && value=32235;

Can I select more counts in one query?

I have a table with a field called type, where 3 various rows are possible: 1, 2 and 3.
Now, I don't care about 3 at all. I need to count how many rows there are with type = 1 and with type = 2. I am doing this with 2 queries, like this:
Query1: SELECT COUNT(id) as count FROM users WHERE type='1'
Query2: SELECT COUNT(id) as count FROM users WHERE type='2'
Can I do this with only 1 single query? If so, should I, or not? How would the query look?
SELECT type,
COUNT(id) AS count
FROM users
WHERE type IN ('1','2')
GROUP BY type
SELECT type, COUNT(id) AS count
FROM users
GROUP BY type
HAVING type < 3
SELECT sum(case when type = '1' then 1 else 0 end) as count_for_one,
sum(case when type = '2' then 1 else 0 end) as count_for_tow
FROM users
WHERE type IN ('1','2')
If you want separate numbers,
SELECT SUM(IF(type='1', 1, 0)), SUM(IF(type='2', 1, 0))
FROM users WHERE type IN ('1', '2')

Categories