Hi I need help this question:
Calculate and display every delegate’s no & name along with their attained credits versus the course’s code, name & credits.
delegate (StudentNumber,StudentName,Phone)
session (Code,Date,Room)
take (StudentNumber, CourseCode, Grade)
course (CourseCode, CourseName,CourseCredits,)
module (ModuleCode, ModuleName, Price,Credits,Course_Code(FK))
Here's what I have so far:
SELECT no,name FROM delegate
INNER JOIN module,take
ON module.code = take.code
INNER JOIN course
ON CourseCredits = Credits
That's all I have so far, any help would be appreciated, i know i have to use a sub query but not too sure where
SELECT no,name FROM delegate
INNER JOIN take ON take.StudentNumber = delegate.StudentNumber
INNER JOIN module ON module.code = take.code
INNER JOIN course ON CourseCredits = Credits
Does it help ?
Related
I have seen several posts about this on Stack Overflow, but none of them seems to give me an answer that I can understand.
I am trying to join several relations together in order to get all of the relevant information to output all routes that start in China and end in the United States.
In the SeaRoute relation, the start_port and end_port are stored as INT and in the Port relation the pid corresponds to the start_port and end_port and includes a pcountry column.
I am starting off with just trying to output everything that has a start_port that is in China. I am expecting 3 results from my Record relation as those are the only ones that start with China in the table; However, I am receiving 6 records at the output (all of the results appear to have been doubled if I go back and audit what's in the table).
While I want the right answer, I am more concerned that I have a fundamental misunderstanding of Inner Join and the other Join methods. What am I doing wrong?
SELECT *
FROM Record
INNER JOIN Goods AS Go_data
ON Record.gid = Go_data.gid
LEFT JOIN SeaRoute AS SR
ON Record.rid = SR.rid
RIGHT JOIN (SELECT pid, pcountry AS starting_port_country
FROM Port
INNER JOIN SeaRoute AS SR ON Port.pid = SR.start_port
WHERE Port.pcountry = 'China')
AS start_port_table ON SR.start_port = start_port_table.pid
From the looks of your query, you want to be INNER JOINing between the records that you have only on the routes that you want.
You know all of the SeaRoutes that start in China and end in the United States already, you do however need to join to the Ports table twice like so:
SELECT sr.rid,
sp.pcountry AS starting_port_country,
ep.pcountry AS end_port_country
FROM dbo.SeaRoute sr
INNER JOIN dbo.Port sp ON sp.pid = sr.start_port
INNER JOIN dbo.Port ep ON ep.pid = sr.end_port
WHERE sp.pcountry = 'China'
AND ep.pcountry = 'United States'
Then you just need to join that to your main query:
SELECT *
FROM Record
INNER JOIN dbo.Goods AS Go_data ON Record.gid = Go_data.gid
INNER JOIN
(
SELECT sr.rid,
sp.pcountry AS starting_port_country,
ep.pcountry AS end_port_country
FROM dbo.SeaRoute sr
INNER JOIN dbo.Port sp ON sp.pid = sr.start_port
INNER JOIN dbo.Port ep ON ep.pid = sr.end_port
WHERE sp.pcountry = 'China'
AND ep.pcountry = 'United States'
) ports ON ports.rid = Record.rid
There's no way I can explain joins to you any clearer than this page can:
https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
Good morning, I am trying to perform the following query in MySQL:
Show the name and surname of the consultants who have not participated in any "Juan Perez" project.
I'm using the following query:
SELECT consultant.name, consultant.surname FROM consultor
INNER JOIN participate ON participate.id_consultant = consultant.id
INNER JOIN project ON project.id = participate.id_project
INNER JOIN cliente ON client.id = project.id_client
WHERE client.name NOT IN("Juan Perez")
But when I execute the query, it only hides those that are directly related in the tables.
How could I hide the other records where the consultants appear so that they do not appear?
Thanks.
I believe it should work if on the conditions of the join to client you exclude the client name there that it will return you what you want.
SELECT consultant.name, consultant.surname
FROM consultor
INNER JOIN participate ON participate.id_consultant = consultant.id
INNER JOIN project ON project.id = participate.id_project
INNER JOIN client ON client.id = project.id_client and client.name NOT IN ("Juan Perez");
I managed to solve it in the following way. Thanks to all for the help.
SELECT consultant.name, consultant.surname FROM consultant
LEFT JOIN (
SELECT id_project, participate.id_consultant FROM participate
INNER JOIN project
USING(id_project)
INNER JOIN client ON client.id = project.id_cliente
WHERE client.name like 'Juan Perez'
)
project ON project.id_consultant = consultant.id
WHERE project.id_project IS NULL;
I'm trying to get my head around this SQL question:
A database for a hotel chain contains the following tables:
Hotel(HotelNo, HotelName, City)
Room(RoomNo, HotelNo, Type, Price)
Booking(HotelNo, GuestNo, DateFrom, DateTo, RoomNo)
Guest(GuestNo, GuestName, GuestAddress)
I want to List the details of all rooms at the Grosvenor Hotel,including the name of the guest staying in the room, if the room is occupied.
I'm okay with joining 2 tables in SQL but I don't know how to go about joining 4 tables.
My attempt would probably be:
SELECT Room.*, Guest.GuestName
FROM Room
INNER JOIN Hotel, Booking, Guest
ON Hotel.HotelName = "Grosvenor Hotel", Hotel.HotelNo = Room.HotelNo, Booking.GuestNo = Guest.GuestNo;
I think that's completely wrong but anyway, hopefully someone knows what I should be doing. Thanks in advance
The correct syntax is:
SELECT Room.*, Guest.GuestName
FROM Room
INNER JOIN Hotel on Hotel.HotelNo = Room.HotelNo,
inner join Booking on Booking.hotelno= Hotel.HotelNo
inner join Guest on Booking.GuestNo = = Guest.GuestNo
where Hotel.HotelName = "Grosvenor Hotel"
Try this :
select g.roomno, g.guestname
from hotel h join room r on h.hotelno = r.hotelno
join booking b on b.hotelno=r.hotelno
join guest g on g.guestno=b.guestno
where h.hotelname='Grosvenor Hotel';
you can also try this .. It Will help You
SELECT Room.*, Guest.GuestName FROM Room
INNER JOIN Hotel on Hotel.HotelNo = Room.HotelNo,
join Booking on Booking.HotelNo= Hotel.HotelNo
join Guest on Booking.GuestNo = Guest.GuestNo
where Hotel.HotelName = "Grosvenor Hotel"
I try to write a query in order to retrieve full info for our product in virtuemart 2.
First I try with single INNER JOIN like the example bellow:
$query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`
FROM `pmqbw_virtuemart_products_el_gr` AS p
INNER JOIN `pmqbw_virtuemart_product_categories` ON p.`virtuemart_product_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_product_id`
LIMIT 0 ,10";
The above worked well!
Then I tried to be more complex and I wrote the following:
$query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`, `pmqbw_virtuemart_categories_el_gr`.`category_name`
FROM `pmqbw_virtuemart_products_el_gr` AS p
INNER JOIN p
ON `pmqbw_virtuemart_product_categories`.`virtuemart_product_id`=p.`virtuemart_product_id`
INNER JOIN `pmqbw_virtuemart_categories_el_gr` AS x
ON x.`virtuemart_category_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_category_id`
LIMIT 0 ,10";
But something goes wrong here and I cannot understand.
After that I'll try to retrieve more tables and more joins in order to bring product data I need.
Thank you for your help in advance!
Actually I want to retrieve data from the following tables.
From pmqbw_virtuemart_products_el_gr I want fields: product_name, product_s_desc,virtuemart_product_id`
From pmqbw_virtuemart_product_categories I want fields: virtuemart_product_id, virtuemart_category_id
From pmqbw_virtuemart_categories_el_gr I want fields: virtuemart_category_id, category_name
From pmqbw_virtuemart_product_medias I want fields: virtuemart_product_id, virtuemart_media_id
pmqbw_virtuemart_medias I want fields: virtuemart_media_id, file_url, file_url_thumb where file_mimetype=image/jpeg AND file_type=product
I tried a different query with left joins but does not work as well.
$sql = "SELECT N.product_name,I.virtuemart_media_id,M.virtuemart_product_id,MN.category_name
FROM pmqbw_virtuemart_categories_el_gr AS M
LEFT JOIN pmqbw_virtuemart_products AS P ON P.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_products_el_gr AS N ON N.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_product_medias AS I ON I.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_product_categories AS MN ON MN.virtuemart_category_id = M.virtuemart_category_id
WHERE M.virtuemart_category_id = 1";
Any help will be appreciated!
Thank you!
Thank you all for the help!
the query that runs ok is the following:
$sql = "SELECT DISTINCT X.virtuemart_product_id, X.product_name, M.category_name, Y.virtuemart_product_id, W.file_url
FROM pmqbw_virtuemart_products_el_gr AS X
INNER JOIN pmqbw_virtuemart_product_categories AS P ON P.virtuemart_product_id = X.virtuemart_product_id
AND P.virtuemart_category_id =2
INNER JOIN pmqbw_virtuemart_categories_el_gr AS M ON M.virtuemart_category_id = P.virtuemart_category_id"
INNER JOIN pmqbw_virtuemart_product_medias AS Y ON Y.virtuemart_product_id = X.virtuemart_product_id
INNER JOIN pmqbw_virtuemart_medias AS W ON W.virtuemart_media_id = Y.virtuemart_media_id;
The bud thing is that every product has two or more images and I only want one.
I have to think something in order to eliminate it!
If anyone has something in his mind for help will be more than welcome!
Thank you!
Ok, I find a solution with GROUP BY Y.virtuemart_product_id
:)
Thank you all !
I have a query that displays the modules that are available for a student to enrol on and shows them in a list.. I would like it if the modules that student has enrolled on do not show in the list. Once a student has enrolled on a module it is stored in a table called tbl_studentModule which has two fields, regNum ($studentID) and moduleID.
What do I need to add to this query to only show the modules that are not already in the tbl_studentModule with the relevant regNum.
EG I would lik the query to say something to the effect where the combination of regNum and moduleID do not already exist if tbl_studentModule.
Here is what I have so far...
SELECT DISTINCT am.moduleID, m.moduleName, am.type, s.regNum, s.award
FROM tbl_awardModules am
LEFT OUTER JOIN tbl_module m ON am.moduleID = m.moduleID
INNER JOIN tbl_awardLevels al ON am.awardLevelID = al.awardLevelID
INNER JOIN tbl_award a ON al.awardID = a.awardID
INNER JOIN tbl_student s ON a.awardID = s.award
LEFT JOIN tbl_studentModules sm ON s.regNum = sm.regNum
WHERE am.type <> 'C' AND sm.regNum = '$student_id' AND m.moduleName <> 'A_null_Choice' AND m.moduleName NOT LIKE '%Not Running%'
Hope that makes sense...
Ok, I sussed it..
I added
AND NOT EXISTS (SELECT * FROM tbl_studentModules WHERE regNum = '$student_id' AND moduleID = am.moduleID)
to the end if the query so now it only shows the rows of the table that do not already contain that combination of values