hello I am trying to join a table Classes & Employee together by using the foreign key "Employee_Id" from the C table to get the first and last name from the E table
SELECT class_id,
subject_name,
classes.employee_id
FROM schooladmindb.classes
INNER JOIN schooladmindb.employee
ON
where classes.employee_id = employee.employee_id;
Im unsure of the statement I would have to use to just gain "First_Name" & "Last Name" From the E table which keeping the C table data intact
Your statment is wrong because you dont need the WHERE to join both tables
Check JOIN sintaxis
SELECT class_id,
subject_name,
classes.employee_id
FROM schooladmindb.classes
INNER JOIN schooladmindb.employee
ON classes.employee_id = employee.employee_id;
Yes you can simply add first name and last name along with other fields like
SELECT class_id,
subject_name,
employ_FirstName, // Column in your employee table
employ_LastName,
classes.employee_id
FROM schooladmindb.classes
INNER JOIN schooladmindb.employee
ON classes.employee_id = employee.employee_id;
Related
What currently happens is it just selects students from CA and students who like to skateboard. I need it to return only students who are both from CA and play soccer.
SELECT *
FROM schooldata a
INNER JOIN studentinfo b
ON b.schooldata_id = a.id
WHERE a.state = "ca"
AND ( activity = "soccer"
OR activity = "skateboard" )
You will have to do inner join based on lastname and firstname column ex:-b.lastname=a.lastname and b.firstname=a.firstname . ideally you should be maintaining primary key column of type integer in schooldata table and its foreign key reference in studentinfo and join based on those columns.
You should use join clauses. And I think base on your question. Inner Join is the best clause you should use.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
I have two tables as follows (in mysql):
Table: invoice
# Column Name
1 Id
2 invoice_date
3 invoice_no
4 consigned_to
5 invoiced_to
6 ...
Table: company
# Column Name
1 Id
2 title
3 ...
Both consigned_to and invoiced_to columns on first table are referencing company.Id.
What I am trying to achieve is a query with following columns
Column Name Table Name
Id (invoice)
invoice_date (invoice)
invoice_no (invoice)
consigned_to (invoice)
consigned_title (company.title)
invoiced_to (invoice)
invoiced_title (company.title)
I need unique column names for the consigned_title and invoiced_title columns, because I should be able to query those columns with titles from company table.
I managed to join single column like this with an alias:
SELECT invoice.*, company.title as consigned_title
from invoice
INNER JOIN company ON invoice.consigned_to = company.Id
but could not managed to reference the same column from company for joining with the invoice.invoiced_to. Is it even possible?
You need table aliases:
SELECT i.*, cc.title as consigned_title, ci.title as invoiced_title
FROM invoice i INNER JOIN
company cc
ON i.consigned_to = cc.Id INNER JOIN
company ci
ON i.invoiced_to = ci.id;
If you rename the id column from the company table for the two use cases then you avoid the need to use range variables (OK, so SQL still forces you to assign range variables -- I'm using c and i in this case -- but you don't need to use them, which is an important distinction):
SELECT invoice_no, consigned_title, invoiced_title
invoice
NATURAL JOIN
( SELECT id AS consigned_to, title AS consigned_title FROM company ) c
NATURAL JOIN
( SELECT id AS invoiced_to, title AS invoiced_title FROM company ) i;
I have table employee which contains about 9 columns . ' id,name,etc..'
and I have another table 'onCall' contain 3 columns 'employee_id,department_id and rank '
what I want is to retrieve the employee data who is registered as OnCall employee on this department
I try this to get the employee data :
Select * from employee where id in (SELECT employee_id FROM onCall where department_id = 3)
But like this I can't know what is the rank of the onCall employee , is he registered as primary or backup ,how can I merge rank column from onCall table but only for the selected employee by the id
I tried to join them but I get syntax error
any way to solve this ?
This calls for an inner join
select EMP.*, OC.*
from EMPLOYEE EMP
inner join ONCALL OC
on OC.EMPLOYEE_ID = EMP.ID
where OC.DEPARTMENT = 3
This may help you and use the required columns with table alias name
Select e.*, o.* from employee e
LEFT JOIN onCall o ON o.employee_id = e.id
AND o.department_id = 3
I have 3 tables, First one has the product ID and Name, Second one has Supplier ID and name, In the 3rd one i have product ID and Supplier ID. While displaying, i want to replace the product ID and supplier ID in the 3rd table with product name and supplier name from the 1st and 2nd table respectively.
Please let me know the query for executing it.
Reference: http://dev.mysql.com/doc/refman/5.7/en/join.html
SELECT * FROM table1
INNER JOIN table2
ON table1.id=table2.id
INNER JOIN table3
ON table2.id=table3.id;
If your tables are named products_master, stockists_master, and stockist_product_offer, then you can join the tables and select any of the six columns that you want.
SELECT product_master.name, stockists_master.name
FROM products_master
INNER JOIN stockist_product_offer
ON product_master.id = stockist_product_offer.product_id
INNER JOIN stockists_master
ON stockist_product_offer.stockist_id = stockist.id;
you have to join the tables on IDs so the query is :
let s say that :
* first table : product
* second table : supplier
* third table : match
SELECT P.PRODUCTNAME
S.SUPPLIERNAME
FROM
PRODUCT P
INNER JOIN
MATCH M
ON P.PRODUCTID = M.PRODUCTID
INNER JOIN
SUPPLIER S
ON S.SUPPLIERID = M.SUPPLIERID
ORDER BY 1
;
I am trying to optimise my php by doing as much work on the MySQL server as possible. I have this sql query which is pulling data out of a leads table, but at the same time joining two tags tables to combine the result. I am looking to add a company which is linked through a relations table.
So the table that holds the relationship between the two is relations_value which simply states (I add example data)
parenttable (companies) | parentrecordid (10) | childtable (leads) | childrecordid (1)
the companies table has quite a few columns but the only two relevant are;
id (10) | companyname (my company name)
So this query currently grabs everything I need but I want to bring the companyname into the query:
SELECT leads.id,
GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status,
leads.probability
FROM `gs_db_1002`.leads
LEFT JOIN ( SELECT *
FROM tags_module
WHERE tagid IN ( SELECT id
FROM tags
WHERE moduleid = 'leads' ) ) as b
ON leads.id = b.recordid
LEFT JOIN `gs_db_1002`.tags as c
ON b.tagid = c.id
GROUP BY leads.id,
leads.status,
leads.probability
I need to be able to go into the relations_values table and pull parenttable and parentrecordid by selecting childtable = leads and childrecordid = 1 and somehow join these so that I am able to get companyname as a column in the above query...
Is this possible?
I have created a sqlfiddle: sqlfiddle.com/#!2/023fa/2 So I am looking to add companies.companyname as column to the query.
I don't know what your primary keys and foreign keys are that link each table together.. if you could give a better understanding of what ID's are linked to eachother it would make this a lot easier... however i did something that does return the correct result... but since all of the ID's are = 1 then it could be incorrect.
SELECT
leads.id, GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status, leads.probability, companyname
FROM leads
LEFT JOIN (
SELECT * FROM tags_module WHERE tagid IN (
SELECT id FROM tags WHERE moduleid = 'leads' )
) as b ON leads.id = b.recordid
LEFT JOIN tags as c ON b.tagid = c.id
LEFT JOIN relations_values rv on rv.id = b.recordid
LEFT JOIN companies c1 on c1.createdby = rv.parentrecordid
GROUP BY leads.id,leads.status, leads.probability