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;
Related
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
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 have a table which contains student_id, course_id and result. The table structure is as follows
I need to get course_id which the result is 0 and another condition is that the result may not be 1 for that course.
In that image the course_id 633 has both result 0 and 1. so that the particular course_id Do not get from that table.
Regards,
John
I hope this is what you are looking for:
SELECT student_id,course_id,SUM(CASE WHEN result=1 THEN 1 ELSE 0 END) as 0Count
FROM TableName
GROUP BY student_id,course_id
HAVING 0Count=0
SQL Fiddle
Explanation:
Query will returns students with result=0 and who do not have a result=1 for that course.
Sample Output:
student_id course_id result
3061 663 0
3061 663 1
3062 664 1
3063 665 0
The result will be:
STUDENT_ID COURSE_ID 0COUNT
-----------------------------------
3063 664 0
SELECT course_id FROM table_name WHERE result <> 1
edit: unused selections where kicked
Ah I see. I think you are looking for this code
SELECT course_id FROM table_name WHERE result = 0 AND result NOT LIKE "%1%";
Let me know how is it work for you.
Try this query:
SELECT DISTINCT t1.course_id
FROM table_name t1
WHERE t1.result = 0
AND NOT EXISTS (
SELECT 1
FROM table_name t2
WHERE t2.course_id = t1.course_id
AND t2.result = 1);
Try this
SELECT *
FROM TABLE_NAME
WHERE course_id NOT IN
(SELECT course_id
FROM TABLE_NAME
WHERE RESULT=0
AND course_id IN
(SELECT course_id
FROM TABLE_NAME
WHERE RESULT=1))
I want to remove duplicate lines with the same badge_id and we check the user_id :
For example, with the following data:
id user_id badge_id badge_slot
1 2 ACH_1 0
2 1 ACH_1 0
3 1 ACH_1 0
4 1 AAAAA 0
How to delete ??
If you want to keep the records with the lowest id you can use min()
delete from your_table
where id not in
(
select * from
(
select min(id)
from your_table
group by user_id, badge_id
) x
)
And in MySQL you have the problem that you can't select from the same table that you are deleting from. But you can overcome this by a subselect.
Try this:
DELETE * ,count(*)as n your_table by badge_id HAVING n>1 .
hope it help!
Try this ..
ALTER IGNORE TABLE table ADD UNIQUE KEY idx1('badge_id','user_id');
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