sql : combining 2 query to give 1 resultset - mysql

In a case where I have to perform 2 queries, I wanted to replace it by simply one single line of query. Example:
select col1, col2 from tableA where col3 = 'a';
This will return (consider) 2 rows:
col1 col2
abc abc.bcd
xyz xyz.pqr
Now, in a second table we made a different query for each rows from query 1:
select col1 from tableB where col2 = 'abc';
(AND)
select col1 from tableB where col2 = 'xyz';
This will give a result set like:
TableB
col1
1111
2222
If the question is unclear kindly mention I shall try to elaborate with better example.
(although database vendor is not issue, I am comfortable with oracle or mysql. Thanks).

You basically just need a join between the two tables like this:
SELECT b.col1, a.col1
FROM tablea a
INNER JOIN tableb b ON a.col1 = b.col2
WHERE a.col3 = 'a'

Try this:
select col1
from tableB
where col2 in (select col1
from tableA
where col3='a')

You can use the single query as,
select col1, col2 from tableB where col2 = 'abc' or col2='xyz'

It' s not completely clear to me, but I think you are looking for a usual Join expression?
SELECT B.Col1, A.Col1 FROM TableA A inner join TableB B on A.Col1 = B.Col2
Please refer to the Join expression. This example is for SQL Server and newer versions of Oracle.

You can make a single query like :
select col1 from tableB where col2 = 'abc' or col2 = 'xyz';
And if you have large number of strings to check for then you can use :
select col1 from tableB where col2 in ('abc','xyz','mno');
Update: you can use nested queries like :
select col1 from tableB where col2 in (select col1 from tableA where col3='a');
But make sure that data type of col1 inside that nested query and data type of col2 after where match.

Related

mysql storing subquery in column and performing where condition

i am performing a sub query in mysql which is like
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
how should i get data in where clause. IS it POSSIBLE
One way to do this is :
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
As you see there I have created on derived table t for your query, now result of your query is treated as Table(temp table) and having columns as col1,col2 and col3, Using this result set we can able to access col3 in where clause .
Note - assuming that select col3 from table2 returns single value as per OP's comments
Use cross join:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;

MySQL Query - not interested in using inner query

I have a query say,
select col1,col2 from table1;
which return 2 columns of multiple rows. I want to use these two values in where condition of another query. Something like
select col3,col4 from table2 where col5=col1 and col6=col2;
where col1 and col2 are the resultant values of the first query.
Currently I have used inner query something like
select col3,col4 from table2
where col5 in (select col1 from table1)
and col6 in (select col2 from table1);
But I dont want to use inner query like the one shown above as it slows down bring results.
Please suggest.
JOIN them instead of using IN's like so:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN
(
SELECT col1, col2
FROM table1
) t1 ON t2.col5 = t1.col1 AND t2.col6 = t1.col2
Note that, you didn't need to select specific columns in the second table. You can JOIN the second table table1 directly like so:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN table1 t1 ON t2.col5 = t1.col1
AND t2.col6 = t1.col2

Select from two tables

I need to select several columns from 2 tables:
Tab1
| Col1 | Col2 | Col3
Tab2
| Col1 | Col4 | Col5
This is my query:
SELECT A.*,B.Col4
FROM Tab1 A,Tab2 B
WHERE A.Col1=B.Col1
AND A.Col2=='XXX'
AND A.Col3>5;
However this query concatenates two tables in some manner and duplicates some rows. What is wrong with it?
The expected result is this one:
Tab3
| Col1 | Col2 | Col3 | Col4
There is no problem with the query you posted, but if the problem is that there are
duplicates some rows
As you described then use DISTINCT:
SELECT DISTINCT A.*, B.Col4
FROM Tab1 A, Tab2 B
WHERE A.Col1 = B.Col1
AND A.Col2 = 'XXX'
AND A.Col3 > 5;
Or the other JOIN syntax:
SELECT DISTINCT A.*,B.Col4
FROM Tab1 A
INNER JOIN Tab2 B ON A.Col1 = B.Col1
WHERE A.Col2 ='XXX'
AND A.Col3 > 5;
You should use = instead of ==.
If col1 is primary key of Tab1 and col2 is primary key of Tab2 and relation betweeen Tab1 and Tab2 is one to one, there is no way to duplicate record.
SELECT A.*,B.Col4
FROM Tab1 A,Tab2 B
WHERE A.Col1=B.Col1
AND A.Col2 ='XXX'
AND A.Col3>5;
SQL INNER w3schools
use that web side to practice and to understand inner, left, right join. Multiple queries ....
Use join
SELECT A.*,B.Col4
FROM Tab1 A JOIN Tab2 B ON A.Col1=B.Col1
WHERE A.Col2='XXX'
AND A.Col3>5;
beware that in sql there is no == (double equal sign) to check equality instead use single (=)

MySQL - Update siblings with same key

Couldn't find a solution yet... although it is probably a newbie question, I haven't been able to overcome... hope someone can give a hand.
I have a MySQL table that has:
Blockquote
Col1 Col2
Row1 A null
Row2 A A1
Row3 A null
Row4 B null
Row5 B B1
etc
> Blockquote
How do I construct an SQL update to update Col2, so that the values on Col2 replace the null, ie, C2R1 and C2R3 gets A1, and C2R4 gets B1 ?
You can calculate the required values as follows:
CREATE TEMPORARY TABLE yourtemptable AS
SELECT yourtable.col1, T1.col2
FROM yourtable
JOIN
(
SELECT col1, MAX(col2) AS col2
FROM yourtable
GROUP BY col1
) T1
ON yourtable.col1 = T1.col1
You can then either drop/truncate the original table and recreate it using these values, or if you can't drop the table you can instead perform a multi-table update.
Although it might not work (because the MySQL documentation states that Currently, you cannot update a table and select from the same table in a subquery.), you should try with something like:
UPDATE table1 SET col2 = (SELECT t2.col1 FROM table1 t2 WHERE t2.col1 = table1.col1 AND NOT t2.col2 IS NULL LIMIT 1) WHERE table1.col2 IS NULL

mysql exclude problem

table a
_______________________________
id col1 col2 col3 ...........col20
1 ............................
2 ............................
3 ............................
table b
_______________________________
id colA colB colC colD ...... colZ
query
________________________________
select a.*, b.* from a left join b on b.id = a.col20 where a.id = 1;
In this query table a and b has same column name.
And I need both of them.
select a.id as a_id .. b.id as b_id .. from a left join b on b.id = a.col20 where a.id = 1;
How to avoid typing all column name?
As far as I know there is not an easy way to select * and exclude columns, and it requires putting the full column list, but I'm sure there are other possibilities.
One way of doing this which would make those a.*, b.* type queries work, but requires some initial setup, is to create a view for the table which aliases all of the columns.
the view of a would be a select query with all of the column names aliased.
create view aview as
select id as a_id,
col1 as a_col1,
col2 as a_col2,
...
...
from a
Then anywhere else you could do something like this:
select a.*, b.*
from aview a
left join bview b on b.b_id = a.a_col20
where a.a_id = 1
If the example were that simple and you really only had 2 tables, it would be sufficient to just make a view for one of them.
Hackish, maybe.. I'd probably look to permanently change the column names on the base tables.