The query below will select all of the name records from table 1, which are NOT IN table 2:
SELECT t1.name
FROM t1
WHERE t1.name NOT IN (SELECT t2.name from t2)
This works fine for complete matches of the name field, but it does not work for partial matches. How can I modify the query so that it will select only the name records from table 1 where no part of the name appeals in table 2?
I suspect that the answer is going to involve using INSTR, but I'm not sure of the syntax. Thanks.
JOIN will do the job
CREATE TABLE test1 (
name VARCHAR(10);
)
CREATE TABLE test2 (
name VARCHAR(10);
)
INSERT INTO test1 VALUES
( 'x123' ),
( 'y123' ),
( 'z123' );
INSERT INTO test2 VALUES
( '123' ),
( '423' ),
( '23' );
SELECT t1.name, t2.name
FROM test1 t1
JOIN test2 t2
ON INSTR(t1.name, t2.name ) = 0
ORDER BY t1.name;
Related
I have two simple tables:
table1 :
id , h_id, role, l_name
1 , 2, 3, test1
1, 2, 4, test1
table2 :
id , h_id, role, l_name
1 , 2, 3, test1
1 , 2, 3, test2
we do not have any primary or foreign key to compare. there are chances that id , h_id, role, might have same data in both table but l_name could be different. it cloud be other column as well.
what is best way to compare for above scenario?
If I understood your case, you would be able to isolate data doesn't match from both table1 and table2 ? Is this ? So you can try this query :
select t1.id, t1.h_id,t1.role, t1.l_name, t2.id, t2.h_id,t2.role,
t2.l_name from table1 t1
left join table2 t2
on t1.id = t2.id and t1.h_id =t2.h_id and t1.role=t2.role and t1.l_name=t2.l_name
where t2.id is null
union
select t2.id, t2.h_id, t2.role, t2.l_name, t1.id, t1.h_id, t1.role,
t1.l_name from table1 t2
right join table2 t1
on t1.id = t2.id and t1.h_id = t2.h_id and t1.role=t2.role and t1.l_name = t2.l_name
where t2.id is null;
I would use the MINUS operator:
Select * from table1
Minus
Select * from table2
And repeat the other way
I'm able to run the following:
SELECT * FROM t1 WHERE t1.id NOT IN (SELECT id FROM t2);
As well as:
SELECT * FROM (SELECT id FROM t2 UNION SELECT id FROM t3);
But MySQL complains when I attempt to run the following:
SELECT * FROM t1 WHERE t1.id NOT IN (SELECT id FROM t2 UNION SELECT id FROM t3);
The error is "unexpected 'SELECT'" for the third select.
Is something wrong with my syntax? Is this not possible? I'm open to rewriting using EXISTS if that's the only way.
They are not mistakes on your query:
SQL Fiddle
MySQL 5.6 Schema Setup:
create table t1 ( id int );
create table t2 ( id int );
create table t3 ( id int );
insert into t1 values (1);
insert into t2 values (2);
insert into t3 values (3);
Query 1:
SELECT * FROM t1 WHERE t1.id NOT IN
(SELECT id FROM t2 UNION SELECT id FROM t3)
Results:
| id |
|----|
| 1 |
I have 2 tables as mentioned below
create table #temp(id int, userid int,age int)
insert into #temp values (1,1,1)
insert into #temp values(2,1,2)
insert into #temp values(3,1,3)
create table #tempMOCK(id int, userid int,age int)
insert into #tempMOCK values (6,1,7)
insert into #tempMOCK values (7,1,9)
I want to update the first 2 rows of Mock table on #temp table. I am expecting that age of rowids 2 & 3 should become 7 & 9. I'm using this query but somehow it doesn't work.
UPDATE t1
SET t1.age = t2.age
FROM #temp t1
INNER JOIN #tempMOCK t2 ON t1.userid = t2.userid where t1.id in (1,2)
Since the user id is the same in all rows, you get the same value for the age.
This can be solved using a CTE, like this:
;with cte as
(
select id, userid, age, ROW_NUMBER() OVER(order by id) rn
FROM #tempMock
)
UPDATE t1
SET t1.age = t2.age
FROM #temp t1
INNER JOIN cte t2 ON t1.userid = t2.userid and t1.id = t2.rn+1;
see fiddle here
you can try following query for this:
UPDATE #temp1 set age=(Select t2.age from #tempMOCK t2
where t2.userid=#temp1.userid) where id in (1,2)
Hope it helps
I'm trying to create a view that returns the first occurrence of a value.
I have two tables
First table:
sID | dt
12 | DateTimeValue1
12 | DateTimeValue2
second table:
S_ID
12
I want the view to join both tables and give me the first occurance of S_ID (in this case DateTimeValue1)
How can I accomplish this?
More Info:
in table 1 I have two columns sID and dt. Values for these columns look like this:
sID: 1 dt: 2014-06-12
sID: 1 dt: 2014-06-13
sID 1 dt: 2014-06-14 etc...
I want to join the two tables in my view so that
where S_ID matches sID it returns the first value (in this case 2014-06-12)
Sorry for any confusion!
Here's what I got so far:
This is what I got so far:
CREATE VIEW view_name AS
SELECT [S_ID]
FROM table1
LEFT JOIN table2
ON table1.[S_ID]=table2.sID;
You could do something like : http://sqlfiddle.com/#!3/66ee02/1
create view theview as
select
t1.s_id, min(dt) dt
from
table1 t1 inner join
table2 t2 on t1.s_id = t2.s_id
group by
t1.s_id
In MS SQL Server you can select the first row of table1 and join it with table2 in a view like this:
create view view_name
as
select table1.*,table2.*
from table2
inner join
(select top 1 *
from table1
order by table1.what_ever_field) as table1
on table2.id = table1.id
This works well if table2 has a foreign key to table1.
If they are independent tables with no foreign keys You can do this:
create view view_name
as
select table1.*,table2.*
from (select top 1 * from table2 order by table2.field1) as table2
,
(select top 1 * from table1 order by table1.field1) as table1
Please try :
select t1.sID,min(t1.dt) from first_table t1, second_table t2 where t1.sID=t2.sID group by t1.sID
I tried same with below code in sybase and it worked well.
create table #temp1
(sID int,dt date)
create table #temp2
(sID int)
insert into #temp1
select 12,getdate()
insert into #temp1
select 12,getdate()
insert into #temp1
select 12,getdate()
insert into #temp2
select 12
insert into #temp1
select 13,getdate()
insert into #temp2
select 13
insert into #temp1
select 14,getdate()
insert into #temp1
select 14,getdate()
insert into #temp2
select 14
select * from #temp1
select * from #temp2
select t1.sID,min(t1.dt) from first_table t1,
second_table t2
where t1.sID=t2.sID
group by t1.sID
Im trying to copy row from table to another using 2 coluom only as the tow table schema is not identical ,
am getting this error
Operand should contain 1 column(s)
Any tips whats wrong with my statement ?
Insert table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Where Not Exists (
Select 1
From table1 As T2
Where
(T2.screenname = T1.screenname,T2.list_id = T1.list_id)
)
try to change where condition from (T2.screenname = T1.screenname,T2.list_id = T1.list_id) to (T2.screenname = T1.screenname AND T2.list_id = T1.list_id)
(note AND keyword instead of comma)
Did you try INSERT INTO...ON DUPLICATE KEY syntax?
See MySQL manual here
You can create a unique index in table1 on the columns screenname and list_id
Then use the following statement
Insert ignore into table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Also try this query -
INSERT INTO table1 (screenname, list_id)
SELECT screenname, list_id FROM table2 t2
LEFT JOIN table1 t1
ON t1.screenname = t2.screenname AND t1.list_id = t2.list_id
WHERE
t1.screenname IS NULL AND t1.list_id IS NULL;
Use simple INSERT IGNORE
INSERT table1 (screenname, list_id) SELECT screenname, list_id FROM table2