I have a reference table (main) of products names and a few other tables with alternative names.
At this moment I have 2 tables of alternative names and I display those rows where a FK to the reference table from the table A exist only using
SELECT main.id,main.name,tabA.name,tabB.name FROM main INNER JOIN tabA ON tabA.fk=main.id LEFT JOIN tabB ON tabB.fk=main.id ORDER BY main.name
How to get all rows where a FK exist from any of the alternative tables?
SELECT main.id,main.name,tabA.name,tabB.name
FROM main
LEFT JOIN tabA ON tabA.fk=main.id
LEFT JOIN tabB ON tabB.fk=main.id
ORDER BY main.name
Related
Very often join fields have the same name in joined tables. If just join
SELECT a.*, b.* FROM a INNER JOIN b ON a.id=b.id
it will produce id field twice.
Is it possible to include ALL fields from joined table EXCEPT joined one?
UPDATE
I am using MySQL but standard way is also interesting to me!
UPDATE 2
Regarding USING syntax, how to use it with multiple joins?
SELECT * FROM
a INNER JOIN b USING (b_id)
INNER JOIN c USING (c_id)
swears table b doesn't contain c_id field, which is true, since it is inside a.
Normally I would write
SELECT * FROM
a INNER JOIN b ON a.b_id = b.b_id
INNER JOIN c ON a.c_id = c.c_id
In standard SQL this is achieved through USING
select *
from a
join b using (id);
This will return the id column only once.
I have 3 tables A, B, C
Schema of all 3 tables is same as mentioned below:
1st A table:
cpid ,name, place
2nd B table:
connectorid,dob
3rd C table:
ccpid cconnectorid
Now both tables A and B have many records.
Now some of the records in A and B are with same id.
Now I want to merge the records from A and B into Table C.
Merge logic is as follows
1)If records with cpid = connectorid ,insert into table c.
2)C Table ccpid is the foreignkey for A table cpid and cconnectorid is the foreignkey B table connectorid.
3)Using select query.
You can use select insert with a n inner join
insert into table_c
select a.cpid, b.connectorid, a.place
from table_b as b
inner join table_a as a on a.id = b.id
You can try this solution for your query:
INSERT INTO `C`(`ccpid`, `cconnectorid`, `ccity`)
SELECT ta.`cpid`, ta.`cconnectorid`, tb.`place`
FROM `A` as ta
INNER JOIN `B` tb ON ta.`cpid` = tb.`cconnectorid`
You just need join data from both tables? This is simple JOIN function.
SELECT *
FROM Table_A
INNER JOIN Table_B
ON Table_A.cpid =Table_B.connectorid;
You can insert this select to your Table_C.
Here is INNER JOIN, but I think you should take a look to JOINs, here are examples and you can read more about other JOINs.
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables LEFT JOIN: Return all rows from the left table, and the matched
rows from the right table RIGHT JOIN: Return all rows from the right
table, and the matched rows from the left table FULL JOIN: Return all
rows when there is a match in ONE of the tables
use following query replace with your table names
INSERT INTO CTABLE(ccpid,cconnectorid,ccity)
(SELECT A.cpid ,B.connectorid, A.place FROM
TABLEA A INNER JOIN TABLEB B ON A.cpid = B.connectorid)
I have a database with many tables, I simply want to delete all rows from any table that have a field of 'id_shop' with a value of '2' how can I do this with MySQL, without listing all of the table names in the query?
Thanks
This will work:
Delete a,b,c From tb1 a
Inner join tb2 b
On (a.id=b.id)
Inner join tb3 c
On (b.id=c.id)
I need to create two temporary tables, then join them together into one table and keep all of the column names for both tables and the data. Using Mysql
CREATE TEMPORARY TABLE tenant_invoices
SELECT * FROM invoices
CREATE TEMPORARY TABLE tenant_payments
SELECT * FROM payments
How do I go about doing this using MYSQL?
Thanks.
Once you create the two temp tables, you can use a CROSS JOIN to join the tables together:
select ti.*, tp.*
from tenant_invoices ti
cross join tenant_payments tp
See SQL Fiddle with Demo
CROSS JOIN will work if there is no field to join the tables. If you have a field to join on then you can use an INNER JOIN or LEFT JOIN:
select ti.*, tp.*
from tenant_invoices ti
inner join tenant_payments tp
on ti.account_id = tp.account_id
or
select ti.*, tp.*
from tenant_invoices ti
left join tenant_payments tp
on ti.account_id = tp.account_id
See SQL Fiddle with demo
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.