Create two alias for table - mysql

How can I create two aliases for a table containing a , separator in MySQL?
For example:
SELECT b.*,a.* FROM `tbl_users` a,b where a.id=b.parent_id.

Looks like you are doing some self join and you can do as
select
a.*,b.* from tbl_users a
left join tbl_users b on a.id=b.parent_id
If you only want matching records then make it an inner join instead of left join

Related

Is it possible to include ALL fields from joined table EXCEPT joined one?

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.

How to merge records from two tables into third using MYSQL

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)

How do I do a Join

How do I make a join of the id_curso of these two tables:
In this table id_curso is a FOREIGN KEY from the second table
;
Second Table has id_curso has is Primary_key
For join two table with foreign key related in sample you provided .. inner join
return the subset for row with value common in each of the two table .. in this case with the same id_curso and this value must be present in each table .
If you need a simple inner join
select a.*, b.*
from table1 as a
inner join table2 as b on a. id_curso = b.id_curso ;
You can see a visual rapresenation an this link provided by #MichaelZ
http://imgur.com/hhRDO4d
I hope I didn't make any mistakes in here (using left join):
SELECT * FROM `first_table` LEFT JOIN `second_table` ON (first_table.id_Curso = second_table.id_Curso)
Here is a join statements it depends upon your scenerio
SELECT a.*, b.*
FROM TABLE1 as a
JOIN TABLE2 as b on a.id_curso = b.id_curso ;
SELECT a.*, b.*
FROM TABLE1 as a
LEFT OUTER JOIN TABLE2 as b on a.id_curso = b.id_curso ;
SELECT a.*, b.*
FROM TABLE1 as a
RIGHT OUTER JOIN TABLE2 as b on a.id_curso = b.id_curso ;

Select records from one table where a column value exists in another table

I have 2 MySQL tables A and B.
I would like to select only the records from B where a certain value exists in A.
Example:
A has columns: aID, Name
B has columns: bID, aID, Name
I just want the records from B for which aID exists in A.
Many thanks.
You need to do either INNER JOIN - records that exists in both tables, or use LEFT join, to show records that exists in A and matching IDs exists in B
A good reference:
You need to make a join, and if you don't want to retrieve anything from table b, just return values from table a.
This should work
select b.* from b join a on b.aID=a.aID
Below query will also work and will be effective
SELECT * FROM B
WHERE B.aID IN (SELECT DISTINCT aID FROM A)
You just need a simple inner join between tables A and B. Since they are related on the aID column, you can use that to join them together:
SELECT b.*
FROM tableB b
JOIN tableA a ON a.aID = b.aID;
This will only select rows in which the aID value from tableB exists in tableA. If there is no connection, the rows can't be included in the join.
While I recommend using a join, you can also replace it with a subquery, like this:
SELECT *
FROM tableB
WHERE aID NOT IN (SELECT aID FROM tableA)
You can use join like this.
Select b.col1,b.col2... From tableB b inner join table tableA a on b.field = a.field
Have you tried using a LEFT JOIN?
SELECT b.* FROM tableB b LEFT JOIN tableA a ON b.aID = a.aID

combine two temporary tables into one table, but keep all the column names

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