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
Related
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
Have two mysql tables such as
Table1
Id
Field1
Field2
Table2
Id
LinkId
Field3
Field4
The common fields to link the tables are Table1.Id and Table2.LinkId.
Also important tha Table2 can have multiple rows where LinkId are the same.
So what I have been trying to figure is a mysql query to Select all rows in Table1 that have a linked row or more in Table2 where Field3 contains a certain value. Is this easily possible?
Simply use JOIN
SELECT Table1.*
FROM Table1 A JOIN Table2 B ON A.Id = B.LinkID
WHERE B.Field3 IN ('Your inputs')
You can have multiple tables in the FROM clause:
SELECT *
FROM Table1, Table2
WHERE Table2.Field3 = 'certain value'
AND Table1.Id = Table2.LinkId
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
I have 2 MySQL tables: table1 and table2
The field "table1.name" has records like "category.1298" where the number after the dot comes from the field ID in table2.
I would like to join table2.ID with table1.name to identify whether table2.ID is equal to the number (after the dot) in table1.name
The question is how to eliminate the portion "category." in table1.name
substring_index is what you're looking for:
SELECT *
FROM table1
JOIN table2 ON SUBSTRING_INDEX(table1.id, '.', -1) = table2.id
Use substring_index
Select substring_index(table1.name,'.',-1) returns 1298
select *
from table1, table2
where substring_index(table1.name,'.',-1) = table2.id
i have have two identical tables
lets call them t1 and t2
and i want to show data from both of them BUT they may have identical rows (lets say these rows have the same id )
in that case i only want to get the row from table 1 and ignore the one from second table .
so
SELECT column_name(s) FROM t1 UNION SELECT column_name(s) FROM t2
but how should i handle duplicates ?
you mean this ?
select column1 , column2 from (
SELECT column1 , column2 FROM t1
UNION
SELECT column1 , column2 FROM t2
)t
group by column1
you will have distinct column1 here
If you want to remove duplicates from a SELECT result set, you could use the DISTINCT clause with a sub-query
SELECT DISTINCT * FROM (SELECT value FROM t1 UNION SELECT value FROM t2) AS S
Or better, you might use the UNION DISTINCT syntax:
SELECT value FROM t1 UNION DISTINCT SELECT value FROM t2;
BTW, for UNION the default is UNION DISTINCT (whereas for SELECT, SELECT ALL is the default), so this could be rewritten as:
-- without specifier UNION is implicitly DISTINCT
SELECT value FROM t1 UNION SELECT value FROM t2;
... which is in fact the query you proposed. What was wrong with that? It works with my test set: http://sqlfiddle.com/#!2/d4812/1
Maybe a sqlfeedle with your actual table content might help to provide a better answer.
Here is one way:
SELECT column_name(s)
FROM t1
UNION
SELECT column_name(s)
FROM t2
where not exists (select 1
from t1
where t1.col1 = t2.col1 and t1.col2 = t2.col2 and . . .
)