How to delete NON-duplicate rows from a table? - mysql

I have a table single_post which looks something like below
id | for_post
0 50
1 50
2 100
3 75
How to delete every entry on which for_post value exist only once ?. Which means after executing the query, the last two entries will be removed because for_post exist only once.
I didn't try anything yet because I have no idea what to try. Thanks in advance

How About Something like this:
DELETE FROM single_post
WHERE for_post IN
(SELECT for_post FROM single_post GROUP BY for_post HAVING count(for_post)=1)

Related

How to get exact rows count of particular column in MySQL table

I want to get exact row count of specified column
Example: Table
Name Id Age
_______________________________
Jon 1 30
Merry 2 40
William 50
David
There are 4 rows in table but i want to count ID column.
I am using below query to achieve it
select count(Id) from table;
But its returning 4 and I know why it is returning 4 but I want output as 2 because there are only two rows in Id column.
How can i achieve it?
Try this:
select count(Id) from table where id>0;
with the help of #blabla_bingo and #Edwin Dijk finally i have achieved it by below query
select count(Id) from table where Id!="";

Update a value and a table with a foreign key

ticketId(**)
timeExpected
timeElapsed
187
5
5
225
4
8
856
8
15
782
10
8
**primary key
*foreign key
id(**)
(*)ticketId
beyondTime
1
187
0
2
225
1
3
856
1
4
782
0
I have to know which ticket his out of time and I have this in mind and in my database but I can't figure it out with SQL. I want to know when a ticket is out of time like the ticket number 225 is, I would like to update the other table with a binary 1 for "out of time" and 0 "good".
I don't know if I can update the "beyondTime" table when I do "timeExpected - timeElapsed" in the first table when a ticket exceed the time expected.
First, let's simplify the problem you're looking a solution for:
Comparing two data field values for the rows in a table
I believe that's basically what you're trying to do. And of course, you can make a decision based on the comparison and act accordingly like updating some values in another table if you want to.
But if all you're looking for is kind of a report, then all you need would be just a select statement equipped with the logic doing the comparison for you.
You can use SQL Case Statement for this. Here is an easy guide if you want to take a look.
The select statement you need would be like this:
select ticketId, (case when timeElapsed > timeExpected then 1 else 0 end) as beyondTime from tickets
The result would be like this:
Two things to remember:
Don't get disappointed by the negative points and keep asking questions :)
Identify the issue you need help with and be specific with your question. I'd suggest you to update this question.
Good luck!

Deleting based on compare

I'm sorry if this has already been asked, but I have not found it already answered entirely yet - so I'm hoping someone can help provide some clarification.
I have a delete query in access that I only want to delete certain rows based on if they sum to 0
Ex:
Tom -1
Jill 5
John -3
Tom 2
Jill -3
John 3
I only want to delete John's records because they sum to 0.
I have the delete where exists written -- and it is good about not deleting if none sum to 0, but if even one person sums to 0 it's trying to delete all the records. How can I get it to delete only the ones that sum to 0? Any help would be greatly appreciated.
I would go with something like:
Delete from [TableName] Where [Column1] In (
Select [Column1] From [TableName] Group By [Column1] Having Sum([Column2]) = 0 )
Of course, replace with your own [TableName],[Column1],[Column2]

Can I SUM a certain cell value using a query in MySQL?

Lets assume I have a table with two columns: key and value.
can I return the SUM of all the values that answer the WHERE key = something condition?
For example:
1 30
2 50
1 40
Can I return 70 using a query resembles to this
SELECT SUM(value) WHERE key = 1
Good attempt. You were pretty close. Read Aggregate Function
And you can use the below Query to get the Output.
select sum(values)
from table_name
where key='1'
group by key
yes, you can, just try to do it
check http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_sum

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)