I use as RDBMS the version 5.5.24-log of MySql.
With this sql query in output I have all records presents on the two tables tbl_1 and tbl_2:
SELECT * FROM `tbl_1` CB
JOIN `tbl_2` A ON A.xCode = CB.xCode
AND RIGHT (A.xElement, 1) = CB.xElement
WHERE
CB.xType IN ('A')
AND MONTH(xDate) BETWEEN 1 AND 4
ORDER BY xDate DESC;
Now I need extract from tbl_1 all records not presents in tbl_2 and I have tried this sql query but I have error
Lost connection to MySQL server during query
SELECT i.* FROM
`tbl_1` i
LEFT JOIN `tbl_2` o ON o.xCode = i.xCode_cabina
AND RIGHT (o.xElement, 1) = i.xElement
WHERE
i.xType IN ('A')
AND MONTH(xDate) BETWEEN 1 AND 4
AND o.xElement IS NULL ;
Can you please help me figure out the problem?
Thanks in advance.
Select i.* From tbl_1 i
WHERE i.xType in ('A') and
MONTH(i.xDate) bewteen 1 and 4 and
i.xCode_cabina not exist (select o.xCode from tbl_2 o where o.xElement is null);
Are you looking to do something like this?
SELECT * FROM tbl_1
WHERE xCode NOT IN (SELECT xCode FROM tbl_2)
Source: Mysql: Select rows from a table that are not in another
Related
My platform: Ubuntu 12.04
I have in my database:
- table1: box, tray, ...
- table2: tray, bolt, ...
I've tried this sql query:
select `tray`,bolt from table2 where `tray` in ( select `tray` from table1 where `box` > 11 );
gives me a list of trays and bolts.
I would like to have: box, tray and bolts as output. How?
thanks in advance. A pointer to a good tutorial for extra credit? :-)
You should use an inner join instead of IN() :
SELECT t.tray,t.bolt,s.box
FROM table2 t
INNER JOIN table1 s
ON(t.tray = s.tray and s.box > 11)
As #Reto mentioned, you can read about joins here!
In general: to get data from more then 1 table , you have to either use JOIN, or use a sub query in the select.
A solution with a sub query(correlated query) :
SELECT t.box,t.tray,
(select s.bolt from table2 s where s.tray = t.tray) as bolt
FROM table1 t
WHERE t.box > 11)
You are looking for a JOIN:
select t1.box, tray, t2.bolt
from table1 t1 join
table2 t2
using (tray)
where t1.box > 11;
Am trying to get the all rows from Tabl1 which are not available in Table2 with help of NOT IN MSQL query. But am getting timeout exception and query is not getting executed. Below is the mysql query which I am using.
SELECT * FROM identity WHERE
unique_id NOT IN (SELECT Message_Queue.index FROM Message_Queue);
Could any please tell the reason or any other way for replacement of NOT IN operation?
When you have so many records in the in() clause then you should use a join instead
SELECT t1.*
FROM table1 t1
left join table2 t2 on t2.myId = t1.myId
where t2.myId is null
Because in MySQL NOT IN is less performant, try using EXISTS
SELECT *
FROM identity a
WHERE NOT EXISTS
(
SELECT null
FROM Message_Queue b
WHERE b.index = a.unique_id
);
you should also put an index on those columns.
I'm working on this query on mySQL - it seems to be processed on a website with PEAR and MDB2 on the running server (dont know why… din't do that myself). On my local test system it generates always MDB2 error. PHPmyadmin doesn't do this query as well.
The Subselection is needed because there are not just one but four subselects in this query.
SELECT * FROM table1
WHERE table1.orderID
IN
(
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.customID = table2.customID)
WHERE table1.active=1
)
I can simplify it like that (works):
SELECT * FROM table1
WHERE table1.orderID
IN (1,2,3)
The Subselect works, too:
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.customID = table2.customID)
WHERE table1.active=1
Thank you so much for any help!
The inner query should return exactly one column, like:
SELECT *
FROM table1
WHERE table1.orderID IN
(
SELECT orderId
-- ^^ here
FROM table1
LEFT JOIN
table2
ON table1.customID = table2.customID
WHERE table1.active=1
)
I have a "server" table which has a column named 'SN' in mysql, when do query to retrive servers with some sns from 'sn1' to 'sn10000', we can:
select * from server where sn in ('sn1','sn2','sn3',...'sn10000');
If there is only one sn in 'sn1'-'sn10000' which not exists in database, then the query above will retrive 9999 rows of result.
The question is how can I easily get which one in 'sn1'-'sn10000' is not exists in database except the additional work, such as handling the result with shell script etc.
I have an ugly sql like below can use:
select * from (select 'sn1' as sn
union select 'sn2'
union select 'sn3'
....
union select 'sn10000') as SN
where not exists (select id from server where server.sn=SN.sn);
Is Anyone has other better methods? Thanks.
Your query is perfectly fine for the intended use, but on MySQL the NOT IN and LEFT JOIN/IS NULL are more effecient that NOT EXISTS. Here are your alternatives:
NOT IN
SELECT *
FROM ( SELECT 'sn1' as sn
UNION ALL SELECT 'sn2'
UNION ALL SELECT 'sn3'
....
UNION ALL SELECT 'sn10000') as SN
WHERE sn.sn NOT IN (SELECT s.id FROM SERVER s)
LEFT JOIN/IS NULL
SELECT s.id
FROM SERVER s
LEFT JOIN ( SELECT 'sn1' as sn
UNION ALL SELECT 'sn2'
UNION ALL SELECT 'sn3'
....
UNION ALL SELECT 'sn10000') as SN ON SN.sn = s.id
WHERE sn.sn IS NULL
You might notice I used UNION ALL, rather than UNION - UNION removes duplicates (which won't happen in your example), making it slower so UNION ALL is a better choice.
Stick your sn1, sn2, sn3... sn10000 values in a temporary table, and then use Joins.
Select server.* from server inner join tempt on (tempt.value = server.sn)
will give you the ones that match, where as
Select sn.* from server right outer join tempt on (tempt.value = server.sn)
where server.somefield is Null
should take care of finding the missing ones.
what is the syntax if I want to utilize 2 or more tables for my mysql query.
Example, I'm going to fetch the idnumber from the 1st table and the religion on the 2nd table. And the query will return the combined version of those 2 tables showing only the religion and idnumber.
The code might look something like this , but it doesn't work:
select t1.IDNO, t1.LNAME t2.RELIGION from t1, t2 where t2.IDNO='03A57'
The SQL query would be as follows:
SELECT a.idnumber, b.religion FROM table1 a, table2 b
You can add conditions from both tables as well by doing the following:
SELECT a.idnumber, b.religion FROM table1 a, table2 b WHERE b.religion = 'Christian'
More information can be found in this thread: http://www.astahost.com/info.php/mysql-multiple-tables_t12815.html
SELECT t1.IDNO, t1.LNAME FROM t1 LEFT JOIN t2.RELIGION ON ( t2.IDNO = t1.IDNO )
(more or less)
The Join is the command that will link the two.
http://dev.mysql.com/doc/refman/5.0/en/join.html
The code below would do a cross-join.
SELECT tb1.id, tb2.religion FROM tb1 JOIN tb2 ON (tb1.religion_id = tb2.religion_id) WHERE t2.IDNO='03A57'
Again... see http://dev.mysql.com/doc/refman/5.0/en/join.html...