how to select row when column must satisfy multiple value? - mysql

For example, I've got a table
name | ability
kevin|say
kevin|scream
nike |say
I wanna get only kevin in response, when looking for say and scream. Count of parameters may change.

You can do this with group by and having:
SELECT name
FROM t
WHERE ability in ('say', 'scream')
GROUP BY name
HAVING COUNT(*) = 2;

One way to do it is:
SELECT * FROM YourTableName WHERE name='kevin' AND ability IN ('say','scream');
Another way to do it:
SELECT * FROM YourTableName WHERE name='kevin' AND (ability='say' || ability='scream');
Good luck!!

This should work:
SELECT name FROM tablename WHERE ability IN ('say', 'scream') GROUP BY name
Or:
SELECT DISTINCT name FROM tablename WHERE ability IN ('say', 'scream')
EDIT: Oh, you're looking for names that have both abilities... Then you can do:
SELECT DISTINCT name from tablename WHERE name IN (SELECT name FROM tablename WHERE ability = 'say') AND name IN (SELECT name FROM tablename WHERE ability = 'scream')
Or better yet, you can add a HAVING clause to the first query:
SELECT name FROM tablename WHERE ability IN ('say', 'scream') GROUP BY name HAVING count(*) = 2

Related

from the sample database , How could I make a query where I could search all the region ( region id) where Kivuto Id is duplicated?

I need to fetch the 3 lines as highlighted in the result with green i.e separate region id but same kivuto id.I need to rectify such products so that I could correct the kivuto id's
Try this.
select * from table_name
where kivuto_id in (
select email from table_name
group by kivuto_id
having count(*) > 1
)
You can refer to this as well: Find rows that have the same value on a column in MySQL
You can simply use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.kivuto_id = t.kivuto_id and
t2.region_id <> t.region_id
);
For performance, you want an index on (kivuto_id, region_id).

MySQL look for duplicates on multiple fields

I have a MySQL database with the following fields:
id, email, first_name, last_name
I want to run an SQL query that will display rows where id and email exists more than once.
Basically, the id and email field should only have one row and I would like to run a query to see if there are any possible duplicates
If you just want to return the id and email that are duplicated, you can just use a GROUP BY query:
SELECT id, email
FROM yourtable
GROUP BY id, email
HAVING COUNT(*)>1
if you also want to return the full rows, then you have to join the previous query back:
SELECT yourtable.*
FROM
yourtable INNER JOIN (
SELECT id, email
FROM yourtable
GROUP BY id, email
HAVING COUNT(*)>1
) s
ON yourtable.id = s.id AND yourtable.email=s.email
You'll want something like this:
select field1,field2,field3, count(*)
from table_name
group by field1,field2,field3
having count(*) > 1
See also this question.
You can search for all ids that meet a specific count by grouping them and using a having clause like this:
SELECT id, COUNT(*) AS totalCount
FROM myTable
GROUP BY id
HAVING COUNT(*) > 1;
Anything this query returns has a duplicate. To check for duplicate emails, you can just change the column you're selecting.

MySQL Group by one column if another has changed?

I've been looking to see if there's a MySQL selector that would allow me to select by one column if another has a different value.
Example:
id name value
----------------
1 john 1
2 craig 1
3 john 2
So, what I'd be looking to do is select both rows 1 and 3 since they have the same name and the value has changed.
If this isn't possible, I can parse through all the results in code, but if MySQL can do this for me, even better!
Try this:
Select * from `table` where Name in (
select name from `table` Group by Name having Count(*)>1)
Inner select Looks for names that exists more than one in your table and the outer select get the data for that name.
You could select all the names that have more than value, and then all the rows with those names:
SELECT *
FROM mytable
WHERE name IN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1)
this may work;
select * from table a
where exists
(select b.id from table b
where a.name = b.name and a.value <> b. value)

How to count the number of entries in two selects?

Semi-newbyism ahead: I need to do two selects and count the number of items in both of them. Here's a bad example of what I thought would work --
sum(
select count(*) as count1 from users where name = 'John'
union
select count(*) as count1 from users where name = 'Mary'
) as theCount
(This is, as I said, a BAD example, since I could obviously write this as a single select with an appropriate WHERE clause. In what I really have to do, the two things I have to do are such that I can't do them as a single select (or, at least, I haven't yet found a way to do them as a single select).
Anyway, I think what I'm trying to do is clear: the select-union-select bit returns a column containing the counts of the two selects; that part works fine. I thought that wrapping them in a SUM() would get me what I wanted, but it's throwing a syntax error. The right thing is probably trivial, but I just don't see it. Any thoughts out there? Thanks!
For generic selects that you can't necessarily write with one where:
SELECT sum(count1) as totalcount FROM (
select count(*) as count1 from users where name = 'John'
union all
select count(*) as count1 from users where name = 'Mary'
) as theCount
select count(*) as count1 from users where name in ('John','Mary')
This is another alternative
select ( select count(*) as count1 from users where name = 'John')
+
( select count(*) as count1 from users where name = 'Mary') as total
Another possible solution:
select
sum(if(name='John',1,0)) as tot_John,
sum(if(name='Mary',1,0)) as tot_Mary,
sum(if(name in ('John','Mary'),1,0)) as total
from users

How to group by and order correctly

I have a problem ordering my results correctly when using the group by. It seems to show the first entry in the database instead of the most recent in the group.
Example:
id(autoincrement) | name
1 | anne
2 | james
3 | anne
4 | brad
As you can see I have "anne" entered multiple times which is why I am using the group by. I would then like it to display the "anne" that is the most recent, which would be the entry "3". Instead it displays the first "anne"(1)
My query
"Select * FROM TABLE GROUP BY name ORDER BY id DESC
Any help would be greatly appreciated.
The problem is that you're selecting all the fields (using * is seldom a good idea) so each row is unique therefore there is nothing to group on.
Try:
SELECT
Name,
MAX(ID)
FROM
TABLE
GROUP BY
Name
A possible solution:
SELECT id, name
FROM TABLE
WHERE id IN (SELECT MAX(id) FROM TABLE GROUP BY name)
Try this; it will work:
SELECT * FROM TABLE
INNER JOIN (SELECT MAX(id) AS id
FROM TABLE
group by name)
ids ON TABLE.id = ids.id
Try
SELECT DISTINCT name
FROM table
ORDER BY id DESC
Building on another anwer already provided, this SQL will avoid having to GROUP BY Name:
SELECT
DISTINCT Name,
MAX(ID)
FROM
TABLE