SQL NOT IN Function - sql-function

Iam trying to insert a record and i want to check that it is not already present in the table.
I try
INSERT INTO emp (empno, name)
VALUES(2, 'ram')
WHERE empno NOT IN (select empno from emp);
but it shows error 'incorrect syntax near where'

You can use following query to insert records into emp
If you are inserting one record at a time then following query will work as best as it can ...
insert into emp (empno,empname)
select distinct empno,empname
from ( 2 empno, 'ram' empname ) as a
where a.empname not in ( select empname from emp )
If you are willing to insert multiple records then just find below query
insert into emp (empno,empname)
select max(empno),empname
from ( select 2 empno, 'ram' empname
union
select 3 empno, 'ram1' empname
union
select 4 empno, 'ram' empname
) as a
where a.empname not in ( select empname from emp )
group by empname

You can't have WHERE clause on INSERT statements, you have WHERE clause only with SELECT/UPDATE
If you work with MySQL, you could do something like:
insert into emp (empno, name) values(2, 'ram') ON DUPLICATE KEY UPDATE name = 'ram'
And if you have a unique index on name column, you will be safe

You can use INSERT IGNORE to fail silently if the row exists. It will attempt the insert, but if the key exists it will do nothing.
INSERT IGNORE INTO emp (empno, name) VALUES (2, 'ram')
You might also want to take a look at INSERT ... ON DUPLICATE KEY UPDATE

you'd probably be looking for something like
insert into emp (empno, name)
SELECT 2 , 'ram'
FROM emp
WHERE 2 not in (select empno from emp)
???

Related

alias column names without mentioning column name

I'm trying to get all columns from each table with a prefix in the output, without mentioning all column names specifically in the select statement. Like:
SELECT *
FROM TABLE1 as T1
FULL JOIN TABLE2 as T2
ON T1.number=T2.number
Where I would want to get all column names from table1 and table2 prefixed with "T1" and "T2".
Many thanks in advance!
SELECT
CONCAT('T1', COLUMN_NAME), ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'TABLE1'
ORDER BY 2
UNION
SELECT CONCAT('T2', COLUMN_NAME), ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'TABLE2'
ORDER BY 2

BreezeJS WHERE id NOT IN (SELECT id FROM table2)

How do I write in BreezeJS to achieve something like this in MySQL?
SELECT id
FROM table1
WHERE id NOT IN (
SELECT id
FROM table2
)
I just couldn't figure it out other than breaking this in to 2 separate BreezeJS queries and do a double for loop on the result arrays to get rid of the duplicates.

How to do a LIKE search with another table's values?

I want to do a LIKE search on two tables. One table has a column of search terms and the other table has the column in which to perform the LIKE searches. Here are the tables:
create table #TableA
(
UserName Varchar(50)
)
create table #TableB
(
Department Varchar(50),
Keyword Varchar(50)
)
Insert Into #TableA VALUES('bob_sales')
Insert Into #TableA VALUES('mary_accounting')
Insert Into #TableA VALUES('sammi_accountant')
Insert Into #TableA VALUES('fred_bestSellerEver123')
Insert Into #TableB VALUES('Accounting', 'accounting')
Insert Into #TableB VALUES('Accounting', 'accountant')
Insert Into #TableB VALUES('Sales', 'sales')
Insert Into #TableB VALUES('Sales', 'seller')
I'd like to run a query that uses LIKE %keyword% and gives me:
bob_sales | Sales
mary_accounting | Accounting
sammi_accountant | Accounting
fred_bestSellerEver123 | Sales
Another method, without join, just for fun:
select department,
(select top 1 username from #tablea a
where a.username like '%' + b.keyword + '%') UserName
from #tableb b
SqlFiddleDemo
SELECT
ta.UserName
,tb.Department
FROM TableA ta
JOIN TableB tb
ON ta.UserName LIKE '%' + tb.[keyword] + '%'
/* If needed add COLLATE Latin1_General_CI_AS */
Remarks:
If your data can contains something like: sammi_accountant_accounting you should add DISTINCT to SELECT statement to avoid duplicates.
For bob_sales_accounting bob will appear twice because it belongs to 2 groups.

SQL Join based on three keys

Database is Teradata
I have two table which I am trying to join. Following are the table structures. When I join these table I expect to get two rows as output but getting 4 rows.what is reason for this behavior. Join based on three keys should uniquely identify a row but still getting 4 rows as output. Any help is appreciated.
TableA
Weekkey|segment|type|users
201501|1|A|100
201501|1|B|100
TableB
Weekkey|segment|type|revenue
201501|1|A|200
201501|1|B|200
when I join these two table using the following query i get the following result
select a.* ,b.user
from tablea a left join tableb b on a.weekkey=b.weekkey
and a.segment=b.segment
and a.type=b.type
Weekkey|segment|type|revenue|users
201501|1|A|200|100
201501|1|B|200|100
201501|1|A|200|100
201501|1|B|200|100
Using sql server, here is ddl and sample data along with the query you posted. The output you state you are getting doesn't happen here.
create table #tablea
(
Weekkey int
, segment int
, type char(1)
, users int
)
insert #tablea
select 201501, 1, 'A', 100 union all
select 201501, 1, 'B', 100
create table #TableB
(
Weekkey int
, segment int
, type char(1)
, revenue int
)
insert #TableB
select 201501, 1, 'A', 200 union all
select 201501, 1, 'B', 200
select a.*
, b.revenue
from #tablea a
left join #tableb b on a.weekkey = b.weekkey
and a.segment = b.segment
and a.type = b.type
drop table #tablea
drop table #TableB

Update statement with Join in Informix

I need to select an employee_id from a table
select emp_id
from employee emp, temp_data td where lower(td.supervisor_name) like lower(emp.last_name||emp.firstname)
and insert it into a field in the same table
update emp set emp.supervisor_id = **The value returned from the previous query**
from employee as emp
Inner join temp_table as td
on td.emp_id = emp.emp_id
Is there any way that I can achieve it? The problem is that I am referring to another field of the same table while setting it in the update statement.. Please let me know if there is any other route I could go... I am doing it in Informix.
I think you need to check your data
done following check
table : temp_data table has following data
supervisor_name
------------------------------
kaushal
abc
solanki
def
table : employee table has following values
firstname last_name
------------------------------
ajay jadeja
sachin solanki
kaushal kumar
manish galav
1.
select supervisor_name from temp_data
select firstname from employee
Note the result
1 record named kaushal which is match . So you need to update the the ID of kaushal
2.
select supervisor_name from temp_data
select last_name from employee
1 record named solanki matches. So you need to update the the ID of solanki
see the result
Note how many rows you need to update (here 2)
First try your select query put into update query
then you said it gaves error ..
then Try this :
Modified your select query as below and put it into your update query.
select emp_id
from employee emp, temp_data td
where
( lower(td.supervisor_name) like lower(emp.last_name )
or
lower(td.supervisor_name) like (emp.firstname) )
)
see the result 2 rows update or not ...????????
Try this also it successfully update your data.
MERGE INTO employee as emp
USING temp_data as td
ON emp.emp_id = td.emp_id
and ( lower(td.supervisor_name) like lower(emp.last_name)
or
lower(td.supervisor_name) like lower(emp.firstname) )
WHEN MATCHED THEN UPDATE set emp.supervisor_id = emp.emp_id

Resources