My requirement is like :
select (*ALL columns of table1, [count of rows of TABLE2 where TABLE1.id = Table2.id]
from TABLE1, TABLE2
where TABLE1.status = 'A'
Am now to SQL, please pardon if the question is too simple
select t1.col1, t1.col2, t1.col3, count(t2.id)
from table1 t1
left join TABLE2 t2 on t1.id = t2.id
where t1.status = 'A'
group by t1.col1, t1.col2, t1.col3
Using correlated subquery:
SELECT t1.*
,(SELECT COUNT(*) FROM TABLE2 t2 WHERE t1.id = t2.id) AS Counter
FROM TABLE1 t1
WHERE t1.status = 'A';
Related
I try to do an Case query, but it is not showing the desired success.
SELECT
t1.Id,
t1.amount,
t2.Id,
t1.singlePrice,
CASE t1.number
WHEN NULL THEN t3.number
END
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.Id = t2.fk_id
LEFT JOIN TABLE3 t3 ON t1.number = t3.number
WHERE t2.id = 487
Usually all numbers are in Table3.
But in Table1 there are numbers which are not existing in Table3.
What I want:
If the number is found in Table3 => use number ob Table3 otherwise use ist from Table1.
you should check for t3.number is null
SELECT
t1.Id,
t1.amount,
t2.Id,
t1.singlePrice,
CASE WHEN t3.number IS NULL
THEN t1.numnber
ELSE t3.number
END
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.Id = t2.fk_id
LEFT JOIN TABLE3 t3 ON t1.number = t3.number
WHERE t2.id = 487
Also possible
SELECT
t1.Id,
t1.amount,
t2.Id,
t1.singlePrice,
COALESCE (t3.number, t1.numnber)
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.Id = t2.fk_id
LEFT JOIN TABLE3 t3 ON t1.number = t3.number
WHERE t2.id = 487
This is my statement
select * from table1
where col1 not in (select col2 from table2);
I want to try(but failed)
select t1.* from table1 t1
inner join table2 on t1.col1<> t2.col2
Is there another way to do this?
select t1.* from table1 t1
left join table2 t2 on t1.col1 = t2.col2
where t2.col2 is null
This is standard implementation of missing record search.
To get the records that exists in table1 but does't exists in table2 you need a left join, not an inner join.
Try this:
select t1.*
from table1 t1
left join table2 t2 on t1.col1 = t2.col2
where t2.id is null -- any non-nullable column will do, best to use pk for performance.
You have not defined t2 object and use left join.
SELECT t1.* FROM table1 t1
LEFT JOIN table2 t2 ON t2.col2 = t1.col1 WHERE t2.id IS NULL
If You try to find out the data of t1.c1 (t1=table1, c1=column1) that is exist or not exist in t2.c2 then use flowing script
select t1.c1 from t1 left join t2 on t1.c1=t2.c2
other wise if you want to find only t1.c1 data that is not in t2.c2 then below script is help full
select t1.c1 from t1 where t1.c1 not in (select t2.c2 from t2)
I have three tables Table1, Table2 and Table3 and the following query which deletes rows in Table1
delete from Table1
where EXISTS
(select (1) from Table2
where Table1.col1=Table2.col1
AND Table1.col2=Table2.col2
AND Table1.col3=(select **Table3.col3 from Table3** inner join Table2 on Table3.col1=Table2.col1)
Is this query correct? If not, how to use a third table inside the where condition?
Edit : Also, please explain how to rewrite the query if we want to delete the rows from table2 which itself is joined with table3?
Here's one way to do it:
delete from table1
where (col1, col2, col3) in (
select t1.col1, t1.col2, t1.col3
from table1 t1
join table2 t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2
join table3 t3 on t1.col3 = t3.col3 and t2.col1 = t3.col1
);
SQL Fiddle Demo
Or using EXISTS might be faster:
delete table1
where exists (
select *
from table2
join table3 on table2.col1 = table3.col1
where table1.col3 = table3.col3 and
table1.col1 = table2.col1 and
table1.col2 = table2.col2);
I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server???
the following is what i am trying to do :
Update Table1
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)
any idea??
If you need to use a subquery to perform the UPDATE you can do it this way:
UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
SELECT id, value
FROM table2
) t2
ON t1.id = t2.id
One way is to alias the table:
update t1
set table1.value1 = t2.value1
from table1 as t1
join table2 as t2
on t1.id = t2.t1_id
You should try
UPDATE table1 SET t1.value1 = t2.value2
FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field2
UPDATE Table1 t1
INNER JOIN (
SELECT id, value
FROM table2
) t2 USING(id)
SET t1.value = t2.value
I have 2 tables, call Table1 and Table2. Table1 columns are id,name,col3.
Table2 has columns id, name , col3.
I want to extract all records from Table1 who's id and name (Both to satisfy condition)
not in Table2.
How to do that
Does the below assist:
SELECT
Table1.ID,
Table1.name,
Table1.col3,
Table2.ID
FROM
Table2 RIGHT JOIN Table1 ON (Table2.name = Table1.name) AND (Table2.ID = Table1.ID)
WHERE
Table2.ID Is Null;
How about this...
SELECT t1.*
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.id = t2.id AND t1.name = t2.name
WHERE t2.id IS NULL
I think you require values( id, name) which are not present in other table.
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.id <> t2.id AND t1.name <> t2.name
WHERE t2.id is null
UNION
SELECT t2.*
FROM Table2 t2
LEFT JOIN Table1 t1
ON t1.id <> t2.id AND t1.name <> t2.name
WHERE t1.id is null