I have two tables with same primary key name "codigo",
I try to make a mysql query to combine both results on new one table.
SELECT * FROM recuperacion.inventario, recuperacion.Salidas where codigo='5ae2399f4fbd3';
this query is not work.
You need to join the tables.
SELECT *
FROM recuperacion.inventario
INNER JOIN recuperacion.Salidas USING (codigo)
WHERE codigo='5ae2399f4fbd3';
The USING clause can be used when the joining relationship is the same column name(s) in both tables. This then allows you to refer to that column without qualifying it with a table prefix.
SELECT t1.*, t2.*
-- INTO NewTableName
FROM recuperacion.inventario t1
INNER JOIN recuperacion.Salidas t2
ON t1.codigo = t2.codigo
WHERE t1.codigo='5ae2399f4fbd3'
NOTE: this will get you all columns from both tables. If you only want specific columns you need to edit the select list. Just be sure to use the aliases.
you have to use join
SELECT *
FROM recuperacion.inventario tab1
INNER JOIN recuperacion.Salidas tab2
on tab1.codigo = tab2.codigo
where tab1.codigo='5ae2399f4fbd3'
Use a JOIN syntax for the query:
SELECT *
FROM recuperacion.inventario a JOIN recuperacion.Salidas b
ON a.codigo = b.codigo
WHERE a.codigo='5ae2399f4fbd3';
Related
I want to create View by selecting all data from multiple tables, but the error I got saying that I have duplicate columns
CREATE VIEW All_Data AS
SELECT *
FROM table1 tb1
INNER JOIN table 2 tb2 ON tb1.ID = tb2.ID
INNER JOIN table 3 tb3 ON tb2.ID = tb3.ID
INNER JOIN table 4 tb4 ON tb3.ID = tb4.ID
INNER JOIN table 5 tb5 ON tb4.ID = tb5.ID
INNER JOIN table 6 tb6 ON o.SpecialID = tb6.ID
INNER JOIN table 7 tb7 ON tb6.ID = tb7.ID
LEFT JOIN table 8 tb8 ON tb7.ID = tb8.ID
However, I am still having the same problem. I want to know is there a faster way to do that instead using alias selecting each column one by one.
DISTINCT won't help as its talking about duplicate columns vs duplicate rows.
Its the * that is causing the columns from all tables, with duplicate column names to be returned. You'll need to replace the * with explicit columns and alias them like below if both are needed.
SELECT p.created_date as product_created_date, order.created_date as order_created date .....
Note using a view isn't a needed pattern. Selects of exactly the right result are normally sufficient. Selects on views can suffer in performance as they are more complicated for MySQL to optimize. They are useful if you need an explicit GRANT on the them for a specific user however.
You need to provide the full list of columns that you want to be part of the view. There is no shortcut.
But yes, to ease your work, If you want the comma separated list of the column names of the table then you can use the following query for each table and put their columns in your select query. You will then just need to alias the columns which have same names.
Select listagg('pd.'|| column_name, ',')
Within group (order by column_id)
From user_tab_columns
Where table_name = 'your_table_name_in_capital';
Note that you need to replace 'pd.' for each table with the table alias in your query.
How would I go about writing a query in MySQL on 3 different tables? Here's what I have so far:
SELECT distinct cool_id, example, table1.name
FROM tardis
INNER JOIN table1
ON table1.unique_id = table2.unique_id
AND table1.unique_id='12345'
AND table2.status='active'
Now let's say that there is a column called 'planets' that exists in a 3rd table. How would I add that to this query to select 'planets' in addition to matching the other conditions in my current query? Also, please advise if an INNER JOIN is not the best choice for this.
Just add another INNER JOIN clause:
SELECT columns
FROM table1
INNER JOIN table2 ON table1.col1 = table2.col2
INNER JOIN table3 ON table1.somecol = table3.othercol
The ON condition in the second join can refer to columns from table1, table2, or both.
Is there a quick way to select results based on constraints from one table and use the foreign keys obtained from this to select results in the parent table? Or can you only do this with two separate queries?
So achieves the same as this, but in one query (tableB has a foreign key from tableA):
Query1: SELECT * FROM tableB WHERE columnN LIKE x
Query2: SELECT * FROM tableA WHERE primaryKey LIKE foreignKeyFromQuery1
Select columns in a table and other to get single result mean JOIN.
you can use this:
SELECT <Table>.<Field>, <Table>.<Field> FROM <Table 1>
INNER JOIN <Table 2> ON <Table 1>.<Primary Key>=<Table 2>.<Foreign Key>
WHERE <Conditions>
Based on your need, it looks you need INNER JOIN:
Query1: SELECT * FROM tableB WHERE columnN LIKE x
Query2: SELECT * FROM tableA WHERE primaryKey LIKE foreignKeyFromQuery1
The joins will be like this:
SELECT * FROM tableB INNER JOIN tableA ON
tableB.primaryKey=tableA.foreignKeyFromQuery1 WHERE columnN LIKE x
If you mean joining tables, then you would do it like this:
SELECT table1.column_name, …
FROM table1
JOIN table2 ON join_condition
You use the syntax join table_to_join on join_condition to join tables where references are consistent. Normally the join condition is something like ON table1.id = table2.id. In this way, the data is combined without duplicating rows.
I am a newbie at MySQL..... I am trying to left join 3 tables one contains some_id,name,count,descr and second one has id,some_id,uni_id and the last one has uni_id,price,added,etc So when i try to join these three tables it says that there's no such field named descr
What could be the best way to join these tables without modifying structure of them?
Assuming the following schema:
table1(some_id, name, count, descr), where some_id is the primary key;
table2(id, some_id, uni_id), where some_id is a foreign key to table1;
table3(uni_id, price, added), where uni_id is a foreign key to table2.
All you need to do is a LEFT OUTER JOIN between the three tables:
SELECT *
FROM table1 t1 LEFT JOIN table2 t2 ON (t1.some_id = t2.some_id)
LEFT JOIN table3 ON (t2.uni_id = t3.uni_id)
References:
Left Outer Join
Join Syntax
It would be ideal if you could post the schema for your tables. Without seeing the query, it sounds like you've made a reference to a field that you may have aliased to the wrong table.
At the most basic level, "descr" doesn't exist as you've tried to reference it, but beyond that, its hard to say without seeing the query itself.
SELECT descr
FROM table1
LEFT JOIN table2 ON table2.some_id = table1.some_id
LEFT JOIN table3 ON table3.uni_id = table2.uni_id
Should do the trick.
can u please show me how to query 3 tables using *? thanks
You need to do a join on the tables in order to get the columns of all of them.
Warning: using * to get all columns is bad practice. You should qualify (name) all the columns you need.
Here is an example:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.key2 = t2.key2
INNER JOIN table3 t3
ON t1.key3 = t3.key3
One way you probably don't like:
SELECT *
FROM table1, table2, table3
You have to give way more information.
This generates the Cartesian product of all the three tables.