Selecting all searched IDs in a database - mysql

SELECT name FROM users
WHERE id=(SELECT added_id FROM relations
WHERE status='1' AND adding_id=2);
Have this but I need to search for all the names which match this search.

what you are doing wrong is to use = instead of in clause.
SELECT name FROM users
WHERE id in (SELECT added_id FROM relations
WHERE status='1' AND adding_id=2);

Related

Convert Postgres query with distinct on to MySQL query

I have a table users:
Where id is a primary key.
I want to select all columns, but all usernames should be unique. I don't care which ids will be in an expected result, but anyway I need them. For that I use the following query in Posgres 10:
select distinct on (username) * from users;
That gives me the result I want:
How can I achieve the same, but using MySQL query?
Your query doesn't make sense in Postgres because it lacks an order by. For this query:
select distinct on (username) u.*
from users u
order by username, id desc;
You can write this as:
select u.*
from users u
where u.id = (select max(u2.id) from users u2 where u2.username = u.username);
Assuming id is unique, this will return one row per username.
I believe this is the conversion of that
SELECT id, username FROM users group by id
You can find more info on this link:Converting SELECT DISTINCT ON queries from Postgresql to MySQL
Extra Note: You can use this http://www.sqlfiddle.com/#!9/0bd1a2/1 to test your SQL which maybe helpful for you in converting Postgres to SQL

Select entries appearing in all results from another query

I have a single table.
This table has 2 fields, product IDs and Store IDs. The same product ID can exist with many different Store IDs.
I need to find the products (if any) that are common across all the stores.
I'm having difficulty constructing the correct query, any advice?
You can check distinct store ids count with product id. If distinct Store ids count equal to total stores that will be the product ids you want.
SELECT productID, count(DISTINCT StoreID) as stroes FROM [Table name] GROUP BY productID
HAVING COUNT(DISTINCT StoreID) = (SELECT COUNT(DISTINCT StoreID) FROM [Table name] );
I'm sure you'll get many better answers, but it sounds like you are wanting the reverse of the distinct clause, not sure if this will work though:
SELECT NOT DISTINCT [Product_ID]
FROM TABLENAMEHERE
You could sue count(distinct productID)
select productID
from my_table
group by productID
having count(distinct productID) = (
select count(distinct store)
from my_table )

how to select row when column must satisfy multiple value?

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

select A, B , C group by B with A from the row that has the highest C

I have collected informations from different sources about certain IDs that should match a single name. Some sources are more trustworthy than others in giving the correct name for a given ID.
I created a table (name, id, source_trustworthiness) and I want to get the most trustworthy name for each ID.
I tried
SELECT name, id, MAX( source_trustworthiness )
FROM table
GROUP BY id
this returns th highest trustworthiness available for each ID but with the first name it finds, regarless of its trustworthiness.
Is there a way I can get that right ?
Mysql has special functionality to help:
SELECT * FROM (
SELECT name, id, source_trustworthiness
FROM table
ORDER BY 3 DESC ) x
GROUP BY id
Although this wouldn't even execute in other databases (not naming all non-aggregate columns in the GROUP BY clause), with mysql it returns the first row encountered for each unique value of the grouped by columns. By ordering the rows greatest first, the first row for each id will be the most trustworthy.
Since this question is tagged mysql, this query is OK. Not only is it really simple, it's also quite fast.
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT id, MAX(source_trustworthiness) max_val
FROM TableName
GROUP BY ID
) b ON a.ID = b.ID AND
a.source_trustworthiness = b.max_val

Select all rows that are absent in other table

There are two tables: users1 and users2. They both have name column. I need select all users from users1 that are absent in users2 table.
I can only select all users and iterate them by PHP, checking every in second table.
Is there a way to do it by SQL?
SELECT `users1`.* FROM `users1` LEFT JOIN `users2` USING (`name`)
WHERE `users2`.`name` IS NULL
For maximum performance, be sure you have an index defined on name in both tables.
This can also be done with a subquery (as others have pointed out), but a join will execute much faster.
Maybe you can try to write a sub query like
SELECT *
FROM Users1
WHERE Username NOT IN
(SELECT Username FROm Users2)
Hope this could help
SELECT * FROM users1 WHERE name NOT IN(SELECT name FROM users2)
Depending on your RMDB and data in this tables, you might want to turn all names to lower case:
SELECT * FROM users1 WHERE LOWER(name) NOT IN(SELECT LOWER(name) FROM users2)
select * from users1 where name not in (select name from users2);