I want to groupby the column1 then count it. Now if the column1 and column2 has same value in other row then it will count only as one
example table
column1 column2
2 4
1 1
1 1
3 4
2 4
3 1
1 3
example output
column1 column2 count
2 4 1
1 3 2
3 4 2
As you can see the 2 in column1 is only counted as 1 because the column2 is also same in other row.
How can I do that in mysql or eloquent query?
You can try below - it'll work in mysql
select column1,max(column2),count(distinct column2)
from tablename
group by column1
You can try the query:
select t.column1, t.column2, count(*) as count from (
select column1, column2 from mytables group by column1, column2
order by column2 desc
) as t
group by t.column1
then its result
seems you need the count for distinct column1, max(column2)
select column1, max(column2), count(*)
from (
select distinct column1, column2
from my_table
) t
group by column1
Related
I have a few many rows in mysql table.I want to get the values sorted like rows with value 1 in first row rows with value 2 in second row and so on . My table look like this,
id Columnn1 Column2 name
1 1 1 a
2 2 2 b
3 2 3 c
4 3 2 d
5 3 2 e
I want the result as
a in first row
b,c in second row
d,e in third row
that is order by Columnn1
try this:
select group_concat(name) as res from tbl_name order by column1 group by column1;
Use below query
select id,column1,column2,group_concat(name) from table_name
group by column2 order by column2
use GROUP_CONCAT(name) function returns a string with concatenated and separated by comma non-NULL value from a group.
select group_concat(name) from tbl_name order by column1 group by column1;
I was reading How to update multiple rows with a single query but i have a question i want to know if you can help me.
I want to update multiple columns and rows with a single query but i need them to update like this:
If column1 and column2 (in all rows) == column1 and column2 (in specific row1) then update value in column3 (in all rows). if not equal then value Whatever but if > or < then value Whatever.
Example :
Column 1 ---- Column2 ----- Column3
Row1 2 3
Row2 2 3 (then) Value1
Row3 2 2 (then) Value2
Row4 2 3 (then) Value1
Many thanks
I think what you're saying is that you want to update rows based on matching to the values in a specific row. We can achieve this by doing a left outer join. If the values match then the join succeeds, so 'row1Match' will not be null. If the values don't match then 'row1Match' will be null. We can then use this to drive the update.
If we assume that the table (which I've called MyTable) has an ID column, and that 'Row1' in your example has an ID of 1, then you want something like this:
UPDATE toUpdate
SET Column3 = (CASE WHEN row1Match IS NOT NULL THEN 'Value1' ELSE 'Value2' END)
FROM MyTable toUpdate
LEFT OUTER JOIN MyTable row1Match ON row1Match.Column1 = toUpdate.Column1
AND row1Match.Column2 = toUpdate.Column2
WHERE row1Match.ID = 1
I would like some help that has had me stumped for two days. I need to retrieve data from a database and order it by column1 when it isn't empty and then the rest of the result by column2
column1 column2
1 11
2 12
3 13
14
15
16
Required result
1,2,3,14,15,16
I've tried numerous approaches, my latest failed attempt being
$SQL = "SELECT * FROM table ORDER BY COALESCE(column1, column2) DESC";
and
$SQL = "SELECT * FROM table ORDER BY COALESCE(column1, column2) ASC";
My above SQL is returning NULL value column1 above column2
This should work:
$SQL = "SELECT * FROM table ORDER BY COALESCE(NULLIF(Other,''), column2) DESC";
I saw it here: SQL Coalesce with empty string
coalesce() would only work if the "empty" values in column1 are actually NULL. Empty strings will not trigger a coalesce() operation.
Beyond that, your query will NOT work. You cannot do a select * with two columns and somehow magically get one single column in the result. For this you'll need a UNION query:
(SELECT column1 AS col FROM yourtable)
UNION ALL
(SELECT column2 AS col FROM yourtable)
ORDER BY col
If you want 1 column, you could try a combination of NULLIF and COALESCE, that should account for both empty and null values
SELECT COALESCE(NULLIF(column1, ''), column2) AS COL
FROM table
SQLFiddle Demo
In case you actually want all of the numbers on a single result row, separated by commas, you can use GROUP_CONCAT along with the previous code:
SELECT GROUP_CONCAT(COALESCE(NULLIF(column1, ''), column2)) AS col
FROM table
SQLFiddle Demo2
Old question but this solution worked for me:
$SQL = "SELECT * FROM table ORDER BY COALESCE(NULLIF(column1, ''), column2)";
You should be able to use CASE like so:
SELECT *
FROM table
ORDER BY
CASE WHEN LENGTH(column1) IS >0 THEN column1
ELSE column2 END
ASC
http://dev.mysql.com/doc/refman/5.0/en/case.html
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;
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