Mysql custom where condition - mysql

My table is :
I want to select records who all are fail(result=0) but except who has same course_id result is 1.
For example look my table there are two rows have result=0 but student_id=1 has another row with result=1 so i want skip this record.
Sorry for confusing you.I hope my expecting output explains clearly what I want.
Expecting output is :
2 | 1 | 0

Try this
SELECT student_id, course_id, result FROM tbl WHERE result = 0
AND student_id NOT IN (SELECT student_id FROM table WHERE result =1
and course_id = tbl.course_id )
Above we are selecting all records that have a result =0 only if the student_id and course_id is not found in the sub query with a result=1

Related

Mysql sting to select everything there column only has 1 out of 2 outcomes

have a database that has 2 columns I want to select from Name (I know this needs changing) and Matched.
I would like to run a query that selects the results where Name has the Status "Matched" but does not have the Stats "settled"
From the example below the top row on has Matched so I would like to see it but the Armininia below has both Matched and Settled so I don't want to return it.
SELECT t1.name, t1.status
FROM table t1
WHERE NOT EXISTS (select 1 from table t2 where t2.name=t1.name and t2.status="SETTLED")
and t1.status="MATCHED"
Above is: "I would like to run a query that selects the results where Name has the Status "Matched" but does not have the Stats "settled""
Group by name and set the conditions in the having clause:
select name
from tablename
group by name
having sum(name = 'MATCHED') > 0 and sum(name = 'SETTLED') = 0

Insert empty row after group

I have a table that contains transaction data. The rows with the same 'group_id' are a part of the same transaction. I am running the following SQL query to show all the transactions:
SELECT * FROM transactions
When I run this query I get as expected a list of all the transactions. But this large list makes it difficult to seperate the data with a different group_id from the other data.
For that reason I want to add an empty row at the end of the group_id, so I get:
1
1
(empty row)
2
2
2
instead of:
1
1
2
2
2
Can someone help me with this?
Here is my database:
http://sqlfiddle.com/#!9/b9bf79/1
I do not suggest you do this at all but if you just want to separate two groups you could do this:
SELECT * FROM transactions WHERE group_id = 1
UNION ALL
(SELECT '','','','','','')
UNION ALL
SELECT * FROM transactions WHERE group_id = 2
Obviously this can added to if there are more group ids in the future but it is not a general purpose solution you are really better off dealing with appearance issues like this in application code.
you can use (abuse) rollup.
SELECT *
FROM transactions
group by group_id, id
with rollup
having group_id is not null
this will insert a row with id set to null after each group_id.
mysql will also sort by group_id because of the group by.
The group by id` makes sure that all rows are shown (your schema does not show it, but I assume id is unique? Otherwise you need to add other fields)
However only id will be null in the extra rows. The other columns repeat the value above.
You can filter them like this:
SELECT
id,
case id is not null when true then date else null end as date,
case id is not null when true then group_id else null end as group_id
-- ....
FROM transactions
group by group_id, id
with rollup
having group_id is not null
Alternatively:
select * from
(SELECT *
FROM transactions
union all
select distinct null, null, group_id, null, null,null from transactions
) as t
order by 3,1
but null values are sorted first, so the "gap" is in front of each section

SQL Distinct cannot working if having count clause

I am having trouble with my sql query (Select distinct didnt work).
My sql is :
select distinct
count(T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
The actual Column 1 data only have 3 data,
But the output is 12
Nb :
- Column1 data is having 1 to Many situation with Column2,
Here are the actual data:
Column 1 Column 2
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 D
3 A
3 B
3 C
3 D
Can anyone help me?
Really appreciate for your attention.
Thanks!
You want to count distinct values, so do count(distinct ):
select count(distinct T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
(However, your sample data and the query's data/columns do not match.)
Your sample data, result and query do not match.
However, what your query does is:
Find all records with a column1 = '2017-05-210'.
Count all of these records where column1 is not null (which is true for all these records, as column1 = '2017-05-210').
This results in one number (one row, one column). But you additionally say with DISTINCT that you want to remove any duplicates from your result rows. With one row only there can be no duplicates, so the function is superfluous here.
So think about what you want to count really. You count distinct values (i.e. count ignoring duplicates) with COUNT(DISTINCT column), but COUNT(DISTINCT column1) would return 1 of course, because you are only looking for one value which is '2017-05-210' (or zero in case there is no record matching this value).

Querying the sum for each value in column

I have the table with values as shown in below table:
And I would like to get results as in below table:
I'm trying to query sum of ans1 like this for all rows
`SELECT sub_id, SUM(ans1) FROM booktable WHERE book_id =1 AND sub_id = 4 OR sub_id = 6`
But I'm not getting the expected result as shown in above table, instead I'm getting result for only one value in sub_id column. I'm a beginner in sql, please help. Thanks.
SELECT sub_id, SUM(ans1) FROM booktable
WHERE book_id = 1 AND sub_id IN (4, 6)
Group by sub_id

How to count the number of duplicate entries in a column MySQL

I have two columns in a MySQL table:
bname and count
bname count
---------- ----------
Blah 2
Blah 2
huh 3
lol 1
huh 3
huh 3
I want something like this. I have created the count column but I don't know how to can I show it like the above example. count column is currently empty.
You should use the good old group by...
select bname, count(*)
from mytable
group by bname
This groups the rows by the unique values, and provides the number of rows of each group.
If you want to store it in your table too:
UPDATE myTable updated
JOIN (select bname, count(*) as cnt
from mytable
group by bname) aggregate
ON updated.bname= aggregate.bname
set `count`= aggregate.cnt
note the backticks, I think you need that here
This should set all rows their respective repetition counts.
SQL Fiddle here
If you want to run a query:
select bname,
(select count(*) from t t2 where t2.bname = t.bname) as cnt
from t
This will give you the additional column on each row.
If you want to fill in the value, use update:
update t
set count = (select count(*) from t t2 where t2.bname = t.bname)
This actually changes the value in the table to the count.