I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;
Related
I have two table
Table 1:
Ensemble_ID
Target
Gene_length
miRNA
miRNA_length
mfe
pvalue
position
prediction
no_of_seeds
And in table2
Ensemble_ID
local_ID
I want to display the result in the following order,
Ensemble_ID, local_ID, Ensemble_ID, Target ,Gene_length, miRNA, miRNA_length, mfe, pvalue, position, prediction, no_of_seeds
But i could not join it .. Can some one help ??
This is what you are asking:
SELECT t1.Ensemble_ID AS Ensemble_ID1, t2.local_ID, t2.Ensemble_ID AS Ensemble_ID2,
t1.Target, t1.Gene_length, t1.miRNA,
t1.miRNA_length, t1.mfe, t1.pvalue, t1.position, t1.prediction, t1.no_of_seeds
FROM table1 t1, table2 t2
But it think you want something like this:
SELECT t2.local_ID, t2.Ensemble_ID, t1.Target, t1.Gene_length, t1.miRNA,
t1.miRNA_length, t1.mfe, t1.pvalue, t1.position, t1.prediction, t1.no_of_seeds
FROM table1 t1
INNER JOIN table2 t2 ON (t2.Ensemble_ID = t1.Ensemble_ID)
If you want to join table1 and table2 on Ensemble_ID there is no need to output it twice
I have two database tables - table1 and table2. For some records in table1 i have several rows connected in table2. For most of them i have 3 rows connected, but for some of them i have an extra row with a column value like table2.field='correct'. How can i join table1 and table2 if i want the result to return only the rows from table1 where there is no row in table2 with column value like table2.field='correct' connected to them ? Counting the number of rows from the second table( if num of connected rows < 4 or something like that is not an option).
I tried something like :
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL
but it did not work ofc. because i always have rows with value in field column. For each row in t1 that is connected to t2 i have record rows in t2 where t2.filed='name' and t2.field='type'. I need the rows from t1 that do not have a row connected to them in t2 where t2.field='correct'.
Use NOT IN
SELECT * from t1 WHERE t1.id NOT IN(SELECT id FROM t2 WHERE t2.field = 'correct')
$sql = "SELECT t1.* FROM table1 t1 CROSS JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL ";
SELECT t1.* FROM table1 t1 WHERE NOT EXISTS (SELECT t2.* FROM table2 t2 WHERE t1.id=t2.id_t1 AND t2.field = 'correct')
I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';
Table1 Has Some data as Categories
Table2 Has Some data Which is Realated to table1 Categories
and the relation between two tables is cat_id from table1 and cat_ids from table2.
What I want is?
I need to display all fields in table1 and from table2 I need only the related content i.e id's present in cat_id(table1) and cat_ids(table2)
I am using a query like this select c.* ,cc.* from news_categories cc, news_content c where cc.cat_id = c.cat_ids group by cc.cat_id this gives only common data from table1 and table2.. i need common data and all categories from table1
can anyone help me?
You should use JOIN instead.
SELECT t1.*, GROUP_CONCAT(t2.content_id)
FROM table1 t1
LEFT JOIN table2 t2
ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id
This is for all fields of both tables...
SELECT Table1.*, Table2.*
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
This is for all fields of Table1 and Content field of Table2...
SELECT Table1.*, Table2.full_content
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
you need to use outer join and this case left join .. Have a look at Documentation
Hi I do have an old table and new table with same index/data. TABLE1 and TABLE2
but TABLE1 has got more data than TABLE2. this was maintained by some one and I dont know how this happened. so my question is how do I compare these two table and find which data is TABLE2 missing?? there is almost 200000 datas there so manually doing is not possible...
in PHP:
http://us.php.net/manual/en/function.array-diff.php
in SQL:
SELECT * FROM TABLE1 WHERE id {NOT} IN ( SELECT id FROM TABLE2 )
depending on criteria of comparison
Solution without nested query:
select TABLE1.id from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id where TABLE2.id is null
Did you mean something like this:
SELECT * FROM TABLE1 t1 WHERE NOT EXISTS(SELECT * FROM TABLE2 WHERE t1.id == t2.id)
By same index I am hoping you mean they share a primary key?
SELECT * FROM TABLE1 WHERE username NOT IN (SELECT username FROM TABLE2)