How do I do a Join - mysql

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 ;

Related

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)

Create two alias for table

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

create an sql table of the selected inner join

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"

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

Select all which is not in table

I have a table in MySQL where all employees are listed. I have another where all employees are listed which have to work on a specific day. And now I want to select all employees which have free (or at least are NOT listed in the work-table).
In this fiddle you can see my schema.
A code like SELECT * FROM pf_mitarbeiter WHERE NOT LISTED AS employeeID IN pf_tagesplan_zuteilungen would be super awesome. But I take other versions as well too.
Thank you guys!
Use a LEFT JOIN to join pf_tagesplan_zuteilungen on employeeID with a condition that there's no rows matching pf_mitarbeiter:
SELECT t1.*
FROM pf_mitarbeiter t1
LEFT JOIN pf_tagesplan_zuteilungen t2 ON t2.employeeID = t1.ID AND t2.date = CURDATE()
WHERE t2.ID IS NULL
select *
from A
where not exists
(
select 1
from B
where a.key = b.key
)
Use a LEFT OUTER JOIN to join the two tables. By doing so, you can select all the rows from the pf_mitarbeiter table even though there is no related row in the pf_tagesplan_zuteilungen table.
SELECT
S.*
FROM
pf_mitarbeiter S
LEFT OUTER JOIN pf_tagesplan_zuteilungen T ON (S.ID = T.employeeID)
WHERE
T.ID IS NULL
;
The IS NULL condition restricts the join to only return pf_mitarbeiter rows where there is no pf_tagesplan_zuteilungen row with a matching employeeId.