this is my first post after hundreds of times I've visited your site.
Simple question.
I have Two Tables, indipendent, different in all, with only one common column. We call it ID_Client
I don't have to modify these tables but I want to merge them and make on this a Select Query.
Table A
Col_ID |Col 1 |Col 2 |Col 3 | ----------|---------|---------|---------| 40302025 |50 | 60 | 70 | 40302028 |50 | 60 | 70 | 40302030 |50 | 60 | 70 | 40302055 |50 | 60 | 70 | 40302074 |50 | 60 | 70 |
Table B
Col_ID |Col X |Col Y |Col Z | ----------|---------|---------|---------| 40302025 |ABC | CDE | 7ASE | 40302028 |ABC | CDE | 7ASE | 40302030 |ABC | CDE | 7ASE | 40302055 |ABC | CDE | 7ASE | 40302074 |ABC | CDE | 7ASE |
Table results
Col_ID |Col X |Col Y |Col Z |Col 1 |Col 2 |Col 3 | ----------|---------|---------|---------|---------|---------|---------| 40302055 |ABC | CDE | 7ASE |50 | 60 | 70 | 40302074 |ABC | CDE | 7ASE |50 | 60 | 70 |
I have to query (like "Order", "Limit" and so on) so that the first table is dependent on the second and vice versa.
Example: I want to sort the column 2 of Table A, based on the X column of Table B where ID> 40302030
Example 2: I want to select column 3 of Table A and column B of Table X where ID> 40302030
I do not want the query to solve these examples, but I want to understand what function should I use. I tried UNION, UNION ALL, JOIN but maybe I did not understand the optics of use.
It sounds like "I want to merge two table in one and then I make the query like SELECT, ORDER BY, WHERE etc."
Thanks and sorry for my english
Carlo
In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table. You can use JOINS in the SELECT, UPDATE and DELETE statements to join the MySQL tables. We will see an example of the LEFT JOIN also which is different from the simple MySQL JOIN.
You should JOIN
the two tables. Something like this:
SELECT
a.col_ID,
b.colx,
b.coly,
b.colz,
a.col1,
a.col2,
a.col3
FROM tableA AS a
INNER JOIN tableB AS b ON a.col_ID = b.col_ID
WHERE a.col_ID > 40302030;
See it in action here:
The results:
| COL_ID | COLX | COLY | COLZ | COL1 | COL2 | COL3 |
------------------------------------------------------
| 40302055 | ABC | CDE | 7ASE | 50 | 60 | 70 |
| 40302074 | ABC | CDE | 7ASE | 50 | 60 | 70 |
For sorting add an ORDER BY
clause.
Since the two table relates to each others, then you should JOIN
them.
You might need to use UNION
if you want to merge the columns of the two tables in to one set of columns, and I think this isn't the case. Since you want to display all the columns from tablea and all the columns from the tableb.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With