How to create an sql table of the selected inner join table.
I would not only like to select the inner join of the two tables, but to create the table in the DB which exactly resembles that inner join. How could I achieve this?
Given two tables table1, table2 which you wish to JOIN:
SELECT *
INTO newTable IN 'YourDatabase.mdb'
FROM table1 INNER JOIN table2
ON table1.id = table2.id
You may omit out the IN 'YourDatabase.mdb' phrase if you just want the new table to be in the same database as the one where tables table1 and table2 currently are. The example I gave above is for MySQL and you may have to change it for other flavors of SQL.
to select from only one of two joined tables:
select B.* from myTableA as A inner join myTableB as B on .....
assuming you have a table myTableC that has the same columns as myTableB then
select B.* into myTableC from myTableA as A inner join myTableB as B on .....
thank you for the timed reply.
I could achieve this using,
CREATE TABLE newTable
SELECT table1.column1, table2.column2
FROM table1 INNER JOIN table2 ON
table1.id = table2.id
This is
"CREATE TABLE...............SELECT.... statement"
Related
So here is the thing, I have two tables:
table1 has columns intUsersID, varUsersName
table2 has columns intCouriers, intResponsible
intCouriers (have some numbers of intUsersID that are Couriers), and intResponsible (have some numbers of intUsersID that are Responsible)
In my query I must see User Names of Couriers and of the Responsible persons
something like that:
SELECT
table1.varUsersName 'Couriers',
table1.varUsersName 'Responsible'
FROM
table1
LEFT JOIN
table2 ON table2.intCouriers = table1.intUsersID
And then I need some how to subquery or join this "table1.varUsersName 'Responsible'", to get also 'Reponsible' persons. Please help me.
Should be this
SELECT table1.varUsersName 'Couriers', table2.varUsersName 'Responsible'
FROM table1
INNER JOIN table3 on table1.intUsersID = table3.intCouriers
INNER JOIN table1 as Table2 on table2.intUsersID = table3. intResponsible
SELECT Couriers.varUsersName as "Couriers",
Responsible.varUsersName as "Responsible"
FROM `table2` t2
LEFT JOIN table1 Couriers on Couriers.intUsersID = t2.intCouriers
LEFT JOIN table1 Responsible on Responsible.intUsersID = t2.intResponsible
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.
I am having 2 different tables like table1 and table2 in that i have a common column customer_id and then i want to pick customer_loan from table1 and customer_name from table2 using the common column customer_id and join both and display in my page anyone help me
It is simple
SELECT table1.customer_loan,table2.customer_nam
FROM table1 INNER JOIN table2 ON (table1.customer_id =table2.customer_id)
try this
select table1.customer_loan,table2.customer_nam from table1 inner join table2 on table1.customer_id = table2.customer_id
You need a JOIN, like this:
SELECT table1.customer_loan, table2.customer_nam
FROM table1 JOIN table2
USING (customer_id)
I have 5 Tables like this:
TABLE 1: PRIMARY_KEY,NAME,FK_TABLE2
TABLE 2: PRIMARY_KEY,FK_TABLE3
TABLE 3: PRIMARY_KEY,FK_TABLE4
TABLE 4: PRIMARY_KEY,FK_TABLE5
TABLE 5: PRIMARY_KEY,DIAGRAM_NAME
And what I want is when I search for a name in a search bar, it returns Name from Table1, and also DIAGRAM_NAME from table 5.
The first part is easy:
SELECT `TABLE1`.name
from Table 1
Where `TABLE1`.name LIKE '%$search%'
But for the second part I need your help...
Thank you!
You need to look into using JOIN:
SELECT T.Name, T5.Diagram_Name
FROM Table1 T
JOIN Table2 T2 ON T.FK_TABLE2 = T2.PRIMARY_KEY
JOIN Table3 T3 ON T2.FK_TABLE3 = T3.PRIMARY_KEY
JOIN Table4 T4 ON T3.FK_TABLE4 = T4.PRIMARY_KEY
JOIN Table5 T5 ON T4.FK_TABLE5 = T5.PRIMARY_KEY
WHERE T.Name LIKE '%$search%'
If you want to return the names that don't have matching diagram names, use LEFT JOIN instead.
Good luck.
You are going to need to JOIN the tables using the Primary Key and FK values:
select t1.name, t5.DIAGRAM_NAME
from table1 t1
left join table2 t2
on t1.FK_TABLE2 = t2.PRIMARY_KEY
left join table3 t3
on t2.FK_TABLE2 = t3.PRIMARY_KEY
left join table4 t4
on t3.FK_TABLE3 = t4.PRIMARY_KEY
left join table5 t5
on t4.FK_TABLE4 = t5.PRIMARY_KEY
Where t1.name LIKE '%$search%'
If you need help learning JOIN syntax, here is a great visual explanation of joins.
I used a LEFT JOIN in my example query, which will return all rows from table1 even if there is not a matching row in the remaining tables.
If you know that there is a matching row in all of the tables that you are joining, then you can use an INNER JOIN.
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.