I am trying to get all the products from the Products table, and at the same time retrieve Company_Name from Company table. A common column in both my table is the Company_Id.
I am using this query:
SELECT
products.product_id,
products.product_name,
products.product_desc,
products.unit_price,
products.stock_level,
products.product_image,
products.gender,
products.type_of_acct,
products.product_cname,
products.product_cdesc,
products.company_id,
company.company_name
FROM
products
INNER JOIN
company ON products.company_id = company.company_id
However this only show all the products from a specific company.
I need to show all the products.
It seems you have an optional relationship here, so use LEFT JOIN:
....
FROM Products
LEFT JOIN Company
ON Products.Company_Id = Company.Company_Id
This retrieves all the products whether linked to a valid company or not.
I think you also need to go over your data and check if you have your foreign keys set up right and have the correct data.
You can use LEFT JOIN to get all details from Products table. LEFT JOIN is fetch all records from left table and also fetch matching records from right table.
SELECT Products.Product_ID, Products.Product_Name, Products.Product_Desc, Products.Unit_Price, Products.Stock_Level, Products.Product_Image, Products.Gender, Products.Type_Of_Acct, Products.Product_CName, Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM Products
LEFT JOIN Company ON Products.Company_Id = Company.Company_Id
Left join is best solution for you.
Or you can make one user define function and from there you can retrieve company name like below
SELECT
Products.Product_ID, Products.Product_Name,
Products.Product_Desc, Products.Unit_Price,
Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct,
Products.Product_CName, Products.Product_CDesc,
Products.Company_Id,
dbo.GetCompanyNameFromCompanyID(Products.Company_Id) AS Company_Name
FROM
Products
Try this
SELECT
Products.Product_ID, Products.Product_Name, Products.Product_Desc,
Products.Unit_Price, Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct, Products.Product_CName,
Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM
Products
LEFT JOIN
Company ON Products.Company_Id = Company.Company_Id
This will return you all the products, with its linked company if any, a NULL will be shown under Company.Company_Name otherwise
Related
How can i merge the rows of a teachers qualification from its table to a single row and join it to another so i can search for a given condition to know if a teacher possesses the given degree/certification. Or is there a better approach other than the one i am trying to use? Thanks in advance. Second image.
SELECT
StaffTable.TeacherType,
StaffTable.StaffID,
TeacherQualificationsTable.YearOfGraduation,
TeacherQualificationsTable.SubjectMajorsCombination,
STUFF((SELECT
', ' + [DegreeObtained]
FROM
TeacherQualificationsTable FOR XML PATH('')),1,1,'') AS [DegreeObtained]
FROM
StaffTable
INNER JOIN
TeacherQualificationsTable
ON
StaffTable.StaffID = TeacherQualificationsTable.StaffID
INNER JOIN
AppointmentChronologyTable ON StaffTable.StaffID = AppointmentChronologyTable.StaffID
WHERE
(StaffTable.TeacherType = #TeacherType)
AND (StaffTable.StaffStatus = #StaffStatus)
AND DegreeObtained NOT CONTAINS ('B.Ed (Bachelor of Education)' , 'M.Ed (Master of Education)', 'G.D.E (Graduate Diploma in Education)')
ORDER BY
StaffTable.LastName,
StaffTable.FirstName,
StaffTable.MiddleName
I have a staffTable and a staffQualificationsTable (which will have multiple qualification entries for any given staff). Am required to check if some staff have some given qualifications eg 'B.Ed (Bachelor of Education)' , 'M.Ed (Master of Education)', 'G.D.E (Graduate Diploma in Education)' and return all the staff without it so they will be called to tender theirs or laid off. Please, i hope this is clearer.
I need to have two tables.
First table (tblStudent) has fields student_id,firstname,lastname,class,rollno.
Second table(feeType) has fields class,admissionFee, tuitionFee etc.
I want to show the different fee for each class.
Like class 5 pays 600 as tuition fee where as class 6 will pay 700.
Eg. If class is 5 in first table then only the class 5 row in feeType should be shown ? How do I design tables as such? How do I join two tables? I will be using sql server 2008 and c# as frontend. Thanks from a novice.
This is the simplest type of INNER JOIN:
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class
Your INNER JOIN would only return matching records,for non-matching records you could use a FULL JOIN and WHERE criteria to check.
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class where s.class=null;
Visit here
This will list all the class names and their fees.
SELECT
s.class,
f.admissionFee as admission_fee,
f.tuitionFee as tuition_fee
FROM
tblStudent as s
JOIN feeType as f ON s.class = f.class
GROUP BY
s.class;
I hope this will help you to get the exact result set.
I have an recruiting database that looks like this: Recruiter has many Students. Students have many "contacts" (how many times we have called/emailed etc.)
I want a recruiter to be able to see when the last time each student of theirs was contacted. I'm developing this app in c# and my database is mySQL.
My table names are employee, students, contact_his and the fields I want to join, given one employeeid are employee.idemployee + students.employee_id then join all of those to contact_his.students_id + students.idstudents. But I have no idea how joins work.. My current code looks like this, but it does not like it:
"SELECT students.* FROM admissions.students
WHERE students.employee_id='PASS VARIABLE HERE'
JOIN contact_his ON contact_his.students_id = s.idstudents
WHERE c.date = (SELECT MAX(date);"
SELECT s.*, MAX(h.date) last_contact
FROM students s
JOIN contact_his h ON h.students_id = s.idstudents
WHERE s.employee_id = 'PASS VARIABLE HERE'
GROUP BY s.idstudents
So I have a table Transportation with columns: ClientIDAsSeller, ClientIDAsBuyer..
and a Client table with column ID (Primary key). My C# app gets a client IDs and sets in the Transportation table.
When I press execute result is null
What is problem how can I solve that?
SELECT
Clients.Name, Transportation.TransStart, Transportation.TransEnd
FROM
Transportation
INNER JOIN
Clients ON Transportation.ClientIDAsSeller = Clients.ID
AND Transportation.ClientIDAsBuyer = Clients.ID
You are likely excluding all possible result sets by only doing one join. You probably want to do:
SELECT
S.Name as SellerName
B.Name as BuyerName,
Transportation.TransStart,
Transportation.TransEnd
FROM Transportation
INNER JOIN
Clients S
ON
Transportation.ClientIDAsSeller = S.ID
INNER JOIN
Clients B
ON
Transportation.ClientIDAsBuyer = B.ID
i want to join two tables to get the Doctors name instead of the ID
SELECT Doctors.doct_name,
Shifts.shift_date,
Shifts.shift_day,
Shifts.shift_start,
Shifts.shift_end
FROM SHIFTS
JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
the Column are shown but without any Results ,, no data from the database ?
your query looks ok.
try two seperate queries to validate the basic data:
select * from Doctors order by doct_id;
and
select * from Shifts order by doct_id;
see if you can manually spot the data issue:
Try a LEFT JOIN:
SELECT Doctors.doct_name,Shifts.shift_date,Shifts.shift_day,Shifts.shift_start,Shifts.shift_end
FROM SHIFTS
LEFT JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
Try using left join and also try using Where clause with an id to check if there is really Doctors exist in both tables
SELECT Doctors.doct_name,Shifts.shift_date,Shifts.shift_day,Shifts.shift_start,Shifts.shift_end
FROM SHIFTS
LEFT JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
OR
SELECT Doctors.doct_name,Shifts.shift_date,Shifts.shift_day,Shifts.shift_start,Shifts.shift_end
FROM SHIFTS
LEFT JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
WHERE Doctors.doct_id = [insert doctors id here - doctors_id]