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);
Related
I am wondering if there is any way to save the intermediate result or tables in SQL. For example assume you have two different SQL statements that in the first statement you join two tables, then you want to see how many rows the resulting table has. I know there are many ways to do this but I am interested in seeing how this can be done sequentially. Consider the following example:
select * from order_table left join customer_table on order_table.id = customer_table.id
Then I want to see count of number of rows (as an easy example)
select count(*) from table
But I do not know what this table should be. How may I save the result of above query in some logical table or how to refer to what was created before in SQL.
You can use WITH like below:
WITH resultTable as ( select * from order_table left join customer_table on order_table.id = customer_table.id )
select count(*) from resultTable
For this particular example you can simply wrap the original query in a sub-query:
select count(*)
from (
select *
from order_table
left join customer_table on order_table.id = customer_table.id
) as x
If you want to store the result in a physical table (temporary or permanent) then the procedure varies for each rdbms. In SQL Server for example you would use SELECT INTO:
select *
into #temp_table
from order_table
left join customer_table on order_table.id = customer_table.id
you can also use CTE. for your question it will be:
;
with table1 as (
select * from order_table
left join customer_table on order_table.id = customer_table.id
)
select count(*) from table1
GO
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
How can I create a query to SELECT ALL DB WITHOUT duplicates
Like (old DB that is no longer in use c,f,g. basically if it does have eur and has an original name than it is relevant):
a
b
c
ceur
d
f
feur
g
geur
I need it to be like:
a
b
ceur
d
feur
geur
Many thanks...
SELECT DISTINCT
is what you're looking for. See more here.
For instance, let's say you have a table that contains the following rows:
name, city, address, country.
You now wish to get the countries that has been stored, without duplicates. Multiple people might come from the same country, and so the table would most likely have duplicate entries of that country.
How you achieve this is by using the SELECT DISTINCT.
Example:
SELECT DISTINCT country FROM table_name;
What this will do is retreive the country row without duplicates. That way, you can see which countries are actually stored in that table without duplicates.
If you have multiple databases (I don't know if that's what you were getting at), then you will need to perform a JOIN on the relevant tables, given you have access to them all. I would recommend doing a LEFT JOIN if you are to join more than just 1 extra table.
Example:
SELECT DISTINCT table_name.row_name, table_name.row_name2, table_name.row_name3
FROM table_name
LEFT JOIN table_name2 ON table_name.row_name = table_name2.row_name
LEFT JOIN table_name3 ON table_name2.row_name = table_name3.row_name
[...]
WHERE table.row_name = 'value';
Can you query information_schema.TABLES and distinct in the select, plus a predicate to filter out whatever you don't want?
You can do:
select t.*
from t
where name like '%eur'
union all
select t.*
from t
where not like '%eur' and
not exists (select 1 from t t2 where t2.name = concat(t.name, 'eur');
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);
can i use select statements for different tables in a single query
SELECT *
FROM users
WHERE first_name LIKE "%'.$search_string.'%"
OR last_name LIKE "%'.$search_string.'%"
thats from the table ' users' , i want to do the same search on another table called sellers. sellers have the same fields first_name, last_name
Yes, you can
Select column1,column2,column3,column4
From table1,table 2
Where Conditions
If you want to do join between tables, check if theirs PK and FK are equals
Eg:
Select table1Id,table2Id,column3,column4
From table1 t1,table2 t2
Where t1=t2
You can search like this
select * from shirts where find_in_set('1','2,3,4,5,1')