query related to in clause - sql

I have two tables:
table 1
fid
field
queryorder
table 2
fid
field
name
fid and field are same in both tables. I want a query like
select fid
from table 1
where field in table 1 corresponding to name = dell
and should not have queryorder = 1

Your question is not clearly worded, but if I'm reading it right, all you want to show is the fid from table 1, where the "name" field in table2 = dell, and the "queryorder" field from table 1 is not equal to "1".
SELECT
table1.fid
FROM
table1
INNER JOIN
table2
ON
table1.fid = table2.fid
AND table1.field = table2.field
WHERE
table2.name = 'dell'
AND table1.queryorder <> 1
Small piece of advice: you should not name a field "name" -- that's a keyword, and will cause you headaches down the road.

SELECT fid FROM table1 WHERE name='dell' AND fid NOT IN (SELECT fid FROM table2 WHERE queryorder != 1)
It's very hard to tell what you're asking from the question, there. This will give you all fids where the name in table1 is 'dell' and the queryorder in table2 is not 1.

This looks like it's really just a join, but I can't tell what you're looking for exactly..
SELECT DISTINCT(fid) FROM Table1 T1
JOIN Table2 T2 ON T1.fid = T2.fid
WHERE t2.queryOrder != 1 AND T1.name = 'DELL'

select t1.fid
from table1 t1, table2 t2
where t1.fid = t2.fib
AND t1.field = t2.field
and t2.name= 'dell'
and t1.queryorder <> 1

Related

Update row in a table based on multiple rows in another table

I have two tables: table1 and table2:
table1 has columns id and integer
table2 has columns id and boolean
table2 can have multiple rows with the same id
I want to update the integer column of table1 by looking at all rows with the same id in table2 and seeing if any of the boolean values are true. If so I want table1.integer to be 1, else I want it to be 0.
I have tried something like this:
UPDATE table1,
(
SELECT table2.id, Sum(table2.boolean) > 0
) AS 'condition'
from table2
WHERE 1
GROUP BY table2.id) table3
SET table1.integer =IF(table3.condition, 1, 0) where table1.id = table3.id
And it seems to work, but I wanted to ask if there is a nicer/cleaner/more succinct way of updating the rows of table1 according to multiple rows of table2.
I would recommend EXISTS:
UPDATE table1 t1
SET t1.integer = (EXISTS (SELECT 1
FROM table2 t2
WHERE t2.id = t.id AND
t2.boolean
)
);
This can take advantage of an index on table2(id, boolean). With such an index, it should be faster than an approach that uses JOIN and AGGREGATION.
The syntax of your query is MySql like, so you can do a join like this:
UPDATE table1 t1 INNER JOIN (
SELECT id, MAX(boolean) maxboolean
FROM table2
GROUP BY id
) t2 ON t2.id = t1.id
SET t1.integer = t2.maxboolean
If there are ids in table1 without a corresponding id in table2 and you want the integer column for them to be updated to 0 then use a LEFT join:
UPDATE table1 t1 LEFT JOIN (
SELECT id, MAX(boolean) maxboolean
FROM table2
GROUP BY id
) t2 ON t2.id = t1.id
SET t1.integer = COALESCE(t2.maxboolean, 0)

Search for if column has 'a' AND 'b'

Suppose you have two tables... table 1 and table 2.
table 1 columns are name / age / area
table 2 has area / job title
I would like to select area and job title only if the area includes both names 'sarah' and 'Phillip' (has to include BOTH the given names)
One way to do it is a combination of exists, group by having and count:
SELECT area, JobTitle
FROM Table2 t2
WHERE EXISTS
(
SELECT t1.Area
FROM Table1 t1
WHERE t1.Area = t2.Area
AND Name IN('sarah', 'Phillip')
GROUP BY t1.Area
HAVING COUNT(DISTINCT t1.Name) = 2
)
Another method is not quite as flexible as the aggregation method. However, you have two tables, and it might have better performance:
select t2.*
from table2 t2
where exists (select 1 from table1 t1 where t1.area = t2.area and t1.name = 'sarah') and
exists (select 1 from table1 t1 where t1.area = t2.area and t1.name = 'Phillip');
In particular, this can take advantage of an index on table1(area, name).
if you have a unique constraint on table1 for (area, name) and if you have performance problems when using the more general answers, you could experiment with joins - which the DB engine can sometimes optimize more than a correlated subquery:
select t2.*
from table2 t2
join table1 t1a on t1a.area = t2.area and t1a.name = 'sarah'
join table1 t1b on t1b.area = t2.area and t1b.name = 'Phillip';

Many-To-Many - get all records from related record

This is my many-to-many table:
Table3:
ID_TABLE3
ID_TABLE1_FK
ID_TABLE2_FK
Some_Field
Now what I want is to do a select of all records from TABLE2 where ID_TABLE1_FK in TABLE3 = 3. This is my query, and It returns all records, but It adds all fields of TABLE3 at end - WHICH IS NOT DESIRED !! :
SELECT * from TABLE2
JOIN TABLE3 ON TABLE3.ID_TABLE2_FK = TABLE2.ID_TABLE2
WHERE TABLE3.ID_TABLE1_FK= 3
So where am I wrong ?
Just use a regular JOIN and select the columns you really want;
SELECT t2.*
FROM TABLE2 t2 JOIN
TABLE3 t3
ON t3.ID_TABLE2_FK = t2.ID_TABLE2
WHERE t3.ID_TABLE1_FK = 3;
This could conceivably produce duplicates (if they are in TABLE3). So, you might be better off with:
SELECT t2.*
FROM TABLE2 t2
WHERE EXISTS (SELECT 1
FROM TABLE3 t3
WHERE t3.ID_TABLE2_FK = t2.ID_TABLE2 AND t3.ID_TABLE1_FK = 3
);

Multiple SELECT with NOT IN - inner Select statement returns empty and the whole SELECT returns empty

There are two table TABLE1 and TABLE2 in which there is a common field ID. I wanted to retrieve values from TABLE2 that doesnot match in TABLE1 based on ID value.
select * from TABLE2 where subject = 1 and ID NOT IN (select ID from TABLE1 where subject = 1)
Sample:
TABLE1 ID SUBJECT 1 1
TABLE2 ID SUBJECT 1 1 2 1
The expected result is 2 and it works fine.
But when TABLE1 is empty or the inner select ID from TABLE1 where subject = 1 returns empty, the whole select statement returns empty.
But the expected result is 1, 2
Is there any way to achieve this ?
Use a left join
select t2.*
from table2 t2
left outer join table1 t1 on t1.id = t2.id and t1.subject = 1
where t2.subject = 1
and t1.id is null
See a good explanation of joins
I think you can use not exists also for this work -
select * from TABLE2 where subject = 1 and NOT exists
(select 1 from TABLE1 where subject = 1 and table1.id = table2.id)

mysql join two tables

I have a problem joining two tables:
table1
id name
1 aaa
2 bbb
3 ccc
table2
id table1_id name
1 1 x1
2 1 x2
3 2 s1
table1 is the main table, table2 contains attributes.
I need to join and search both tables, but display distinct results from first table.
When using JOIN I get multiple results from table2.
The scenario is I need to search main table TABLE1 and ALL ATTRIBUTES in TABLE2 and return if found
select distinct(name) from table1 inner join table2 on table1.id = table2.table1_id where table2.name = x2;
Should do the trick.
If you need entries which exists in both tables:
SELECT * from Table1 t1
WHERE YourConditionsHere
AND EXISTS (SELECT 1 from Table2 t2
WHERE t1.Id = t2.Table1_id
AND YourConditionsHere)
if you need entries from Table1 for which does not exists enteries in Table2
SELECT * from Table1 t1
LEFT JOIN
(SELECT * from Table2
WHERE YourConditionsHere
) t2
ON (t1.Id = t2.Table1_id)
WHERE YourConditionsHereForTable1
another option
select * from table1 t1 where t1.id in (select table1_id from table2 t2 where t2.name = "x1");
it's probably best to check query plains (i.e. EXPLAIN) for all suggested queries and check the one that performs best for your exact scenario.