SELECT fields where there no exist others in another table - mysql

I have the following table schema:
Table 1
-
field1
Table 2
-
field1 | field2
What I want to do is select field2 from the second table where field1 in the second table doesn't exist in the first table (field1).
I had this :
SELECT t2.field2
, t2.field1
FROM table1 t1
, table2 t2
WHERE t2.field1 != t1.field1
The problem is that this query will retrieve multiple repeated information from table2 if multiple rows apply in table1. I added DISTINCT and/or LIMIT but it still doesn't work. Any idea on how to do this?

You can use a subquery:
SELECT t2.field2, t2.field1 FROM table2 t2 WHERE t2.field1 NOT IN (SELECT t1.field1 FROM table1 t1);
This will give you all rows from table2 that have a field1 which is not in table1.
You can now use DISTINCT or LIMIT on the outermost query for any further processing.

You can use LEFT JOIN together with IS NULL check :
SELECT DISTINCT t2.field2
FROM table2 t2
LEFT JOIN table1 t1 ON t1.field1 = t2.field1
WHERE t1.field1 IS NULL

The title of your question is almost the command you need: 'NOT EXIST'. SQL is so simple. ;)
SELECT DISTINCT t2.field2
FROM Table2 t2
WHERE NOT EXISTS (SELECT * FROM Table1 WHERE t1.field1 = t2.field1)

Related

Can a table get other table column data

I need to create a select statement where the statement need to retrieve data from other table column data
eg.
Table1 Table2
id id2
age age2
Select id, age from table 1 where id= id2
Is that possible.
You can use INNER JOIN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.id = T2.id2
DEMO using INNER JOIN
You can use EXISTS
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE EXISTS(
SELECT 1
FROM Table2 AS T2
WHERE T2.id2 = T1.id
);
You can use IN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE T1.id IN (SELECT T2.id2 FROM Table2 AS T2)
Note:
In the working demo the output consists of two rows. There are two entries in tabel1 and three entries in table2. But there are only two matching entries found between these two tables. That's why output consists of only two rows.
Yes you can. It is called a JOIN and there are several types of JOINs. I suggest you read up on them on SQL JOINs.
SELECT id ,age
FROM TABLE 1
WHERE id IN (SELECT id2 FROM TABLE2);
OR
SELECT id ,age
FROM TABLE1 , TABLE2
WHERE id = id2 ;
OR
SELECT id ,age
FROM TABLE 1 , (SELECT id2 FROM TABLE2) TBL2
WHERE id = TBL2.id2 ;

case usage in mysqli for selecting and joining

Hello all I am having a simple problem while selecting some data from table and then joining the table with another table depending on the value of field in table 1
like i have table1, table2 and table 3.
I want to
select field1,field2 and then check the value of field 3 if field 3 has value = 1 then select field1,field2,field3 from table 2 and join table 1 with table 2 on field1 and field1 else select field1,field2,field3 from table3 and join table1 with table2 on field1 and field1.
I now this can be done case but i am not so comfurtable with it please help me solve the problem ..
I think this can be achieved using a UNION call:
SELECT t2.f1, t2.f2, t2.f3
FROM table2 t2
INNER JOIN table1 t1
ON t1.f1 = t2.f1
WHERE t2.f3 = 1
UNION ALL
SELECT t3.f1, t3.f2, t3.f3
FROM table3 t3
INNER JOIN table1 t1
ON t1.f1 = t3.f1
Try this:
SELECT t1.field1 AS t1field1,
t1.field2 AS t1field2,
CASE WHEN t1.field3 = 1 THEN t2.field1 ELSE t3.field1 END AS t2t3field1,
CASE WHEN t1.field3 = 1 THEN t2.field2 ELSE t3.field2 END AS t2t3field2,
CASE WHEN t1.field3 = 1 THEN t2.field3 ELSE t3.field3 END AS t2t3field3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.field1 = t2.field1
LEFT JOIN table3 t3 ON t1.field1 = t3.field1;

Retrieve data from two tables sql

I am having 2 tables named table1, table2.
table1 columns are t1_id, t1_name, t1_status.
table2 columns are t1_id, t2_id, t2_name, t2_status.
When I do Some operation in frontend t1_id will be inserted into table2.
What I need is I want t1_id(s) from table1 which are not alloted or inserted in table2.
I've tried this query:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id != t2.t1_id
The problem with query is when all t1_id(s) are inserted into table2, then all t1_id(s) are showing again.
How to resolve this issue? (I'am new to sql so please don't consider my mistakes.)
If i understand your question right, this must b the query you need:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
where t2.t1_id is null
Use is null to get rows from table1 which don't have any associations in table2
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
WHERE t2.t1_id IS NULL
I think this answers your questions: Select rows which are not present in other table
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
WHERE NOT EXISTS (
SELECT 1 --it's not so relevant what you put here
FROM table2 t2
WHERE t1.t1_id = t2.t1_id
)
I hope this helps. ;-)

SQL query to find unreferenced records

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

SQL join result fulltext

I have two tables, which I will call table1 and table2. Table1 has 2 fields, id and auth, table2 also has two fields, id and keywords. Note that id of table1 and table2 match.
This is my query:
SELECT id, MATCH(keywords) AGAINST('example') FROM table2 WHERE MATCH(keywords) AGAINST('example')
How am I going to exclude results where the auth (table1) of that same id is not 1?
SELECT id, MATCH(keywords) AGAINST('example')
FROM table2 t2
WHERE MATCH(keywords) AGAINST('example')
AND NOT EXISTS (select 1 from table1 t1 where t1.id = t2.id and t1.auth != 1)
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id != t2.id