Select from two tables - mysql

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 (=)

Related

Sql queries to find all records with no corresponding values

Can someone help me with this please,
I have two tables
TableA with records
A | A1
A | A2
A | A3
B | B1
TableB
A1 | x
A2 | y
I want to retrieve records which do not have any corresponding value in the TableB. Basically, I want to output as follow...
Result :
B | B1
How can write a query?
Thank you.
Definition of tables and sample data:
create table Tbl1 (col1 varchar(10), col2 varchar(10));
insert into Tbl1 values ('A','A1');
insert into Tbl1 values ('A','A2');
create table Tbl2 (col1 varchar(10), col2 varchar(10));
insert into Tbl2 values ('A1','y');
and the query to return desired results (works for both: MS SQL and MySql):
select * from Tbl1
where col2 not in (select col1 from Tbl2 where col1 is not null)
If you are confused by where clause in subquery, this is only to avoid NULL values, as they might introduce unexpected (empty) results.
Use OUTER JOIN:
SELECT
TableA.*
FROM TableA
LEFT OUTER JOIN TableB
ON TableB.Field1 = TableA.Field2
WHERE TableB.Field1 IS NULL
Use EXISTS:
SELECT
TableA.*
FROM TableA
WHERE NOT EXISTS
(
SELECT *
FROM TableB
WHERE TableB.Field1 = Table1A.Field2
)
SELECT *
FROM table_a
WHERE col1 NOT IN (SELECT table_a.col1
FROM table_a
JOIN
table_b
ON (table_b.col1 = table_a.col2))
Use this link for Demo

SQL - Matching two rows from multiple columns in one table to 1 row in another table.

I'm trying to figure out a way in postgres to match two columns from one of my tables to a single column in another table, without getting rid of the first tables columns. I may not explain this so well so here's an example
Table 1: Table 2:
col1 col2 col3 2col1 2col2
1 A B A 13.1
2 A C B 18.3
3 B C C 21.7
4 B D D 11.23
I want to get back a table where col2 and col3 can both match to what's in 2col1, and replace their values with what's in 2col2.
So the result would be:
Table 1:
col1 col2 col3
1 13.1 18.3
2 13.1 21.7
3 18.3 21.7
4 18.3 11.23
Try this:
SELECT t1.col1, t2.2col2 AS col2, t3.2col2 AS col3
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t1.col2 = t2.2col1
LEFT JOIN table2 AS t3 ON t1.col3 = t2.2col1
This can help you. LEFT JOIN is your friend
SELECT
t1.col1,
t2.2col2,
t3.2col2
FROM table1
LEFT JOIN table2 AS t2 ON t1.col2 = t2.2col1
LEFT JOIN table2 AS t3 ON t1.col3 = t3.2col1;

Merging two tables where unique key comprises multiple columns

I have two tables A and B (with the same schema), and I want to merge them by inserting all entries from A into B. If table B already has data associated with a key from A, I want to silently drop those data.
The problem is that table B has a unique key index that consists of three columns, so I can't just say "WHERE A.key <> B.key".
I can't seem to formulate an SQL statement along the lines of:
INSERT INTO B
VALUES ((SELECT * FROM A WHERE ... NOT IN ...))
Is there a way to INSERT those rows from A into B where the corresponding three-column-key doesn't exist in B yet?
INSERT INTO B
(Col1, Col2, Col3, ColN)
SELECT
A.Col1, A.Col2, A.Col3, COlN
FROM A
LEFT JOIN B
ON A.COL1 = B.Col1
AND A.COL2 = B.Col2
AND A.COL3 = B.Col3
WHERE B.Col1 IS NULL
Essentially Join the 2 tables with a left join, and insert all from A where B is null (No corresponding value in B table for a Join on the 3 Key columns)
You could use NOT EXISTS instead of NOT IN
INSERT B
SELECT *
FROM A
WHERE NOT EXISTS
( SELECT 1
FROM B
WHERE A.Key1 = B.Key1
AND A.Key2 = B.Key2
)
Although according to this MySQL optimises LEFT JOIN/IS NULL better than not exists:
INSERT B
SELECT A.*
LEFT JOIN B
ON A.Key1 = B.Key1
AND A.Key2 = B.Key2
WHERE B.Key1 IS NULL

sql : combining 2 query to give 1 resultset

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.

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.