SQL Getting Specific Column other than just the foreign key - mysql

I have sql table called Games as follows:
CREATE TABLE Games
(date date NOT NULL,
ho_t_id varchar(9) NOT NULL,
v_t_id varchar(9) NOT NULL,
h_score int,
v_score int,
PRIMARY KEY(date, ho_t_id, v_t_id),
FOREIGN KEY(ho_t_id) REFERENCES Team ON UPDATE CASCADE,
FOREIGN KEY(v_t_id) REFERENCES Team);
As you can see, it has two foreign keys to a table called Team. The ho_t_id and v_t_id reference to the primary key of the Team table called t_id.
I would like to show all the columns of the Games table (*) but replace the ho_t_id by the name of the team which exist in the Team table. (The Team table contains a column named Name).
How can I do that?

SELECT G.*, T1.NAME as T1_NAME, T2.NAME AS T2_NAME
FROM GAMES G
INNER JOIN TEAM T1 ON T1.T_ID=G.HO_T_ID
INNER JOIN TEAM T2 ON T2.T_ID=G.V_T_ID

You mean this?:
select t.Name, g.allOtherColumns
from Games g
inner join (
select Name, ID
from Team
) t
ON t.ID = g.v_t_id
--where ....

Related

MySQL Query - search for person who has all B

I am trying to solve an SQL question about below question in MySQL
output the patient name who has taken treatment from all doctors at least once.
I am stuck with the "all doctors".
Is there a way to get this result with one query?
Where I have the table,
CREATE table patient (
pno VARCHAR(5) PRIMARY KEY,
name VARCHAR(15) NOT NULL,
);
CREATE table doctor (
dno VARCHAR(5) PRIMARY KEY,
name VARCHAR(15) NOT NULL,
);
CREATE table record (
id INT auto_increment PRIMARY KEY,
dno VARCHAR(5),
pno VARCHAR(5),
FOREIGN KEY(dno) REFERENCES doctor(dno)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY(pno) REFERENCES patient(pno)
ON UPDATE CASCADE
ON DELETE CASCADE
);
pno is the patient number, which is referenced by record,
dno is the doctor's number, which is referenced by record.
I think IN and EXSISTS doen't work in this situation.
Maybe using count(*) would work but, I tried using subquery below, and couldn't solve this.
SELECT count(*)
FROM doctor;
You can:
first select the cardinality of the doctors,
then select all patients whose distinct count of doctors is equal to the cardinality of doctors
hence get the patients' names.
The query should look like this:
SELECT p.name
FROM patient p
WHERE p.pno IN (
SELECT r.pno
FROM record r
GROUP BY r.pno
HAVING COUNT(DISTINCT r.dno) = (
SELECT COUNT(*)
FROM doctors
)
)
Note: I'll leave an SQL fiddle link here in case of question update with some sample data.

sql statement not working getting errors

I am having some trouble getting a statement to run, I am looking for the total credits of each student and the student ID.
I have written:
select student.ID,sum (course.credits)
from (student natural join takes),course
where takes.course_id=course.course_id
group by student.ID
when I run this I get the following error:
column used in NATURAL join cannot have qualifier
my tables are
create table student
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0) check (tot_cred >= 0),
primary key (ID),
foreign key (dept_name) references department
on delete set null
create table takes
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
grade varchar(2),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id,sec_id, semester, year) references section
on delete cascade,
foreign key (ID) references student
on delete cascade
I am not sure what the problem is. when I run this statement it works
select name, sum(course.credits)
from (student natural join takes),course
where ID=1000 and takes.course_id=course.course_id
group by name
if you can point me in the right direction I would appreciate it.. thank you
NATURAL JOIN is not recommended and can lead to subtle unintended bugs, particularly when tables are modified later. Use INNER JOIN instead:
select name, sum(c.credits)
from student s
inner join takes t
ON t.ID = s.ID
inner join course c
ON c.COURSE_ID = t.COURSE_ID
where s.ID = 1000
group by name
Best of luck.
The from clause is just wrong with regard to how you use your brackets. Just use an explicit join clause for all your joins and you should be OK:
SELECT student.ID, SUM(course.credits)
FROM student
NATURAL JOIN takes
NATURAL JOIN course
GROUP BY student.ID

Regarding SQL query 'join' function

I have created the following table but have troubles getting the desired output using the join function. I would like to know that if we have to select car name, price and driver name for cars made in any random year e.g.,vintage = 1995. Any help will be appreciated
create table car
(car_ID NUMBER ,
car_Name CHAR ,
car_Vintage NUMBER,
car_Price NUMBER,
PRIMARY KEY (car_ID));
create TABLE driver
(driver_ID NUMBER,
driver_Name CHAR,
PRIMARY KEY (driver_ID));
create table cardriver
(car_ID NUMBER(3) NOT NULL,
driver_ID NUMBER(4) NOT NULL,
PRIMARY KEY (car_ID,driver_ID),
FOREIGN KEY (car_ID) REFERENCES car(car_ID)
ON DELETE CASCADE,
FOREIGN KEY (driver_ID) REFERENCES driver(driver_ID)
ON DELETE CASCADE);
SELECT
c.car_Name,
c.car_Price,
d.driver_Name
FROM
cardriver as cd
INNER JOIN
car as c on c.car_ID = cd.car_ID
INNER JOIN
driver as d on d.driver_ID = cd.driver_ID
WHERE
c.car_Vintage = #Year
This assumes every car has a driver. Otherwise select from car and left join to cardriver.

How to fetch details and how to connect category and book table structure ( how to use GROUP_CONCAT)

My Problem is
I have table named category with id and category-name
and book with id and book-name
Please write SQL query for fetch result, result should be like
book1- c1,c2,c3
book2- c2,c3
book3-c4
etc.
You need to create an additional table:
CREATE TABLE book_categories (
book_id INT,
category_id INT,
PRIMARY KEY (book_id, category_id),
FOREIGN KEY book_id REFERENCES book (id),
FOREIGN KEY category_id REFERENCES category (id)
)
Then you can use a JOIN to get your result:
SELECT book_name, GROUP_CONCAT(category_name)
FROM book AS b
JOIN book_categories AS bc ON bc.book_id = b.id
JOIN categoriy AS c ON c.id = bc.category_id
GROUP BY b.id

Mysql foreign key

I want to make a link between a table customer and a table product by an IdProduct.
Example:
Create table customer(
idcustomer INT not null,
name Varchar(20),
idproduct INT,
);
create table Product(
idproduct INT not null,
nameProduct varchar(40)
);
How can I link the two together like the foreign key system for, when I select a customer, I can get all his products? It's a question about the structure of the database.
You want to introduce a 3rd table to resolve the many-to-many relationship between customers and products. It should consist of idcustomer and idproduct.
Then, to get all the products for a given customer:
SELECT c.name, p.nameProduct
FROM Customer c
INNER JOIN CustomerProductXref cpx
ON c.idcustomer = cpx.idcustomer
INNER JOIN product p
ON cpx.idproduct = p.idproduct
WHERE c.idcustomer = 12345
In mysql a foreign key is a special type of constraint. It is preferably created with the table, but can also be added afterwards. In this case, you might define the constraint as:
ALTER TABLE customer
ADD FOREIGN KEY (idproduct)
REFERENCES Product (idproduct);
(Note that you have to use the InnoDB engine to take advantage of FK's in mysql. More here
However FK's aren't required to make a JOIN, which is how you would link the tables in a SELECT -
select c.idcustomer, c.name, p.nameproduct
from customer c
join Product p on p.idproduct=c.idproduct;
Here's how you'd make a foreign key constraint (ignoring the cardinality issues that Joe rightly suggests):
CREATE table Product(
idproduct INT not null,
nameProduct varchar(40),
PRIMARY KEY (idproduct )
);
CREATE table customer(
idcustomer INT not null,
name Varchar(20),
idproduct INT,
FOREIGN KEY (idproduct) REFERENCES Product(idproduct )
);
Get your data like this:
SELECT * FROM Product AS P
INNER JOIN Customer AS C ON C.idproduct = P.idproduct
WHERE C.idcustomer = 1