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
)
Related
I need to show only results which are in Table1 and Table2 but are not in Table3. Basically, it should be something like TABLE1, Table2 except INNER JOIN between (TABLE1, Table2) and TABLE3.
Should looks like this - On left side Table1 and Table2, on right side Table3
Now I have this:
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
// And somehow except values which are in Table3
Can somebody help me please? Thanks a lot.
There are a couple different ways to do this. I believe mysql does better with the outer join/null approach:
select t.*
from (
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
) t left join Table3 t3 on t.mesta_email = t3.mesta_email
and t.mesta_kod = t3.mesta_kod
where t3.mesta_email is null
This assume table3 shares the same structure as the other 2 tables.
I would approach the problem almost directly as you write it, using exists and not exists:
select t1.mesta_email, t2.mesta_kod
from table1 t1
where exists (select 1
from table2 t2
where t2.mesta_email = t1.mesta_email and t2.mesta_kod = t1.mesta_kod
) and
not exists (select 1
from table3 t3
where t3.mesta_email = t1.mesta_email and t3.mesta_kod = t1.mesta_kod
);
One advantage of exists/not exists over other approaches involves duplicates. If one of the tables (say table1) has not duplicates, but the others might, there is no need to remove duplicates in the resulting data set.
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 have a weird situation here that I try to resolve through SQL so I don't have to modify much in the application :).
Is there ANY way that I can tell if a column exists in a quere within the query?
(Table2 is not always joined in the query)
SELECT * FROM
Table1 as T1
join Table2 as T2 on t1.id = t2.fk
WHERE
T1.something > 10 OR (IF(table and column exists in the query T2.col, 1, 0);
my alternative is to always join Table2 (painful in this case).
Any SQL guru/genius can help?
I solved the problem with an easy work around. So, for the sake of referencing...
SELECT *
FROM Table1 as T1
WHERE T1.something > 10 OR ((SELECT ....) = 1)
Is it possible to join the results of a SELECT with another table.
Like this:
SELECT * FROM table1 LEFT OUTER JOIN (SELECT * FROM table 2)
I know I need to link the column but am not sure how. Is this possible?
You need to know what columns you are joining on. Assuming they are called ID in both tables, something like this would work:
SELECT *
FROM table1 t1
LEFT OUTER JOIN (SELECT * FROM table 2) t2 on t1.ID = t2.ID
Note that rather than using *, you should name the columns you need explicitly. This will give a more efficient query if you do not need all of the data, and will also prevent duplicate column names from being returned.
You can do this. The code would be something like:
(SELECT id as leftid, [other fields] FROM table1) LEFT OUTER JOIN (SELECT id rightid, [other fields] FROM table2) ON (leftid=rightid)
I didn't test it, though...
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...