How to get unique (not vice-versa) fields on MySQL - mysql

The title may be misleading but i don't know how to formulate it better.
Suppose i have these rows on my MySQL table:
table1:
id column1 column2
1 1 2
2 1 3
3 2 1
4 3 4
I have written a query to retrieve data that have similar vice-versa values on columns column1 and column2 (id: 1 & id: 3), but I'm having trouble querying over data that don't have similar vice-versa rows (id: 2 & id: 4) or that are sort of unique.
EDIT: The query i've used to get vice-versa rows
SELECT * FROM table1 t1
INNER JOIN table1 t2
ON (t1.column1 = t2.column2 AND t1.column2 = t2.column1);

You could use exists logic here:
SELECT id, column1, column2
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE LEAST(t2.column1, t2.column2) = LEAST(t1.column1, t1.column2) AND
GREATEST(t2.column1, t2.column2) = GREATEST(t1.column1, t1.column2));

A simple solution would be to use your query in the WHERE clause to filter out similar rows:
SELECT *
FROM table1 t1
WHERE (column1, column2) NOT IN
(SELECT t1.column1, t1.column2
FROM table1 t1
INNER JOIN table1 t2
ON t1.column1 = t2.column2 AND t1.column2 = t2.column1)
Demo here

Related

SQL Union two tables without relation and return separate columns

I have to tables 'Table1, Table2' which are not related at all, but I need to do a common query with both because a filter.
I thought about this solution:
select * from(
select t1.idT1 from Table1 t1 where idT1 = 1
union all
select t2.idT2 from Table2 t2 where idT2 = 1) as results
but it returns me a single column named idT1 and what I need is two separated columns: 'idT1', 'idT2' because I need to know if id is from table 1 or table 2 to look for their details later.
Is it possible?
select * from(
select t1.idT1, null as IdT2 from Table1 t1 where idT1 = 1
union all
select null as idT2, t2.idT2 from Table2 t2 where idT2 = 1) as results
or as #jarlh suggested
select * from(
select 't1' as T1OrT2, t1.idT1 from Table1 t1 where idT1 = 1
union all
select 't2' as T1OrT2, t2.idT2 from Table2 t2 where idT2 = 1) as results
I am assuming that where IdT1=1 in your question is just an example, because if it is really that then the result will be all 1s, as #Nathan_Sav pointed out.

How to remove common records found in 2 tables?

For example:
table 1
x y
2 3
1 2
8 9
table 2
x y
2 3
8 9
So here I need to remove the common field found in both the table
result should be
Result
x y
1 2
only i should get a unique row
You can use NOT EXISTS :
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.X = t1.X AND t2.Y = t1.Y);
You can also use NOT EXISTS operator which is more efficient as it also considers NULL values while comparison.
select * from table1 A
where not exists (select name from table2 B where A.name=B.name)
Try this:
Demo 1
Demo 2
select x, y
from
(
select x, y
from table1
union all
select x, y
from table2
) i
group by x, y
having count(*) = 1
Assuming the task is "delete duplicated record from BOTH TABLES accounting NULLs":
DELETE t1.*, t2.*
FROM table1 t1
JOIN table2 t2 ON t1.x <=> t2.x
AND t1.y <=> t2.y;
fiddle
Assuming the task is "delete duplicated record from both tables NOT accounting NULLs":
DELETE t1.*, t2.*
FROM table1 t1
JOIN table2 t2 USING (x, y);
fiddle
If the task is "delete duplicated record from one table and store them in another table" then edit DELETE t1.*, t2.* and store only those table which records must be deleted, for example, DELETE t1.*.
Assuming this is to remove duplicate from a table that exist in another table
if all colums have non null value then:-
DELETE t1
FROM tbl_test_1 t1
INNER JOIN tbl_test_2 t2
ON t1.col_1 = t2.col_1
AND t1.col_2 = t2.col_2
WHERE t2.id is not null
Here if a particualr coulm from both table are NULL , we miss that.
So to catch colm match with NULL as well, try this:-
#to check before deletion
SELECT * ,
concat_ws("_",COALESCE(t1.col_1, 'null'),COALESCE(t1.col_2, 'null')),
concat_ws("_",COALESCE(t2.col_1,'null'),COALESCE(t2.col_2,'null'))
FROM tbl_test_1 t1
left JOIN tbl_test_2 t2
ON concat_ws("_",COALESCE(t1.col_1, 'null'),COALESCE(t1.col_2, 'null'))= concat_ws("_",COALESCE(t2.col_1, 'null'),COALESCE(t2.col_2, 'null'))
WHERE t2.id is not null
delete query
#delete query
DELETE t1
FROM tbl_test_1 t1
left JOIN tbl_test_2 t2
ON concat_ws("_",COALESCE(t1.col_1, 'null'),COALESCE(t1.col2, 'null'))= concat_ws("_",COALESCE(t2.col_1, 'null'),COALESCE(t2.col2, 'null'))
WHERE t2.id is not null

joining tables with where clause

I am having two tables, the structure is given below
Table 1
schid
name
cost
type
Table 2
schid
details
oldcost
I am unable to write a query to display records from table 2 of let suppose type A OR B (Here as you can see type field is in table 1), Here one more thing to add is that schid is not a primary key, The query which i am executing is retrieving more records than expected, I think due to join, Can i execute it without using join
SELECT *
FROM Table1
JOIN Table2 ON Table1.schid=Table2.schid
WHERE Table1.type='A'
OR Table1.type='B'
This would help:
SELECT t2.schid, t2.details, t2.oldcost
FROM Table2 t2
JOIN Table1 t1
ON t1.schid = t2.schid
WHERE t1.type IN ('A', 'B');
This should retrieve only the table 2 records which match the criteria.
SELECT t2.*
FROM Table2 t2
JOIN Table1 t1 ON t1.schid = t2.schid
WHERE t1.type = 'A'
OR t1.type = 'B';
SELECT t2.*
FROM `Table2` t2
JOIN `Table1` t1 ON t2.`schid`=t1.`schid`
WHERE t1.`type` IN ('A','B');

Difference between [table1.column1=table2.column1] and [table1.column1=1 AND table2.column1=1]

I don't know if there are any terms for these statements:
I have table1 and table2
table1
id link_id
1 1
1 2
1 3
table2
id link_url
1 www.a
2 www.b
3 www.c
And two different MYSQL Statements:
SELECT table1.id as id, table2.link_url as link_url FROM table1, table2 WHERE table1.link_id =1 and table2.id=1
SELECT table1.id as id, table2.link_url as link_url FROM table1, table2 WHERE table1.link_id=table2.id
I understand that they both return the same results.
Is there any difference in using either of them or doesn't it matter at all?
Yes there is Difference.
First Statement Returns only one row as you have set table1.link_id=1 and table2.id=1
Second Statement will Return every row which have link_id value in table1 similar to id value table2
link_id=table2.id select all data of your table where both coloumn are same .But WHERE table1.link_id =1 and table2.id=1 select data where link_id is 1 and id is 1.... both are diffrent
But USE JOIN is good option :-
SELECT table1.id as id, table2.link_url as link_url
FROM table1 join table2
on table1.link_id=table2.id
WHERE table1.link_id =1

SQL how to compare two columns from two different tables

I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns. I have two columns in table1 that I want to compare them with two columns in table2.
Table 1 have column1 and column2 (that needs to be compared)
Table 2 have column6 and column7 (that needs to be compared)
I need to compare the combination of the two columns. I tried to do the below query however it doesn't work
Select * from table1
where column1, column2 NOT IN (Select column6, column7 from table2)
How can I compare the two columns in the the two tables?
Except shows the difference between two tables (the Oracle guys use minus instead of except and the syntax and use is the same). It is used to compare the differences between two tables. For example, let's see the differences between the two tables
SELECT * FROM
table1
EXCEPT
SELECT * FROM
table2
Try a minus statement. This will give you any results from the first select statement of table1 that aren't in the second select statement on table2.
select column1, column2 from table1
minus
select column6, column7 from table2
NOT EXISTS is a "null safe" version of NOT IN.
If you mean the combination column1 AND column2 not in same row in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6
and table1.column2 = table2.column7)
Or if you mean just column1 and column2 values can't even be in different rows in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6)
and NOT EXISTS (select 1 from table2
where table1.column2 = table2.column7)
The query with the least comparisions I can think of is
Select t1.*
from table1 t1
left join table2 t2 on t1.column1 in (t2.column6, t2.column7)
or t1.column2 in (t2.column6, t2.column7)
where t2.column6 is null
SELECT * FROM table1 t1
RIGHT JOIN table2 t2
WHERE
t1.c1 = t2.c6 AND
t1.c2 = t2.c7
Please try this query:
Select
case when (table1.column1 = table2.column6)
then 1 else 0
end column1_6 check,
case when (table1.column2 = table2.column7)
then 1 else 0
end
from
table1
inner join
table2 on table1.ID = Table2.ID
select * from table1 where column1 not in(select column 6 from table2) or column2 not in(select column7 from table2)
This will give you rows from table1 where there are differences between col1 and col6 or col2 and col7
Hope this helps