This question already has answers here:
How to join two tables mysql?
(4 answers)
Closed 4 years ago.
I have two tables A and B. in Table A i have details of Subject like Subject_code, Subject_name of all the subjects in University. In table B i have details of Students like Roll_Number, Attendance, Subject_code(that that a particular roll_Number has taken etc). Now i want to fetch the Subject_name from table A corresponding to a particular Roll_number from table B;
Only linking from A to B table is the Subject_code.
I am able to get the list of the subject_code from table A as
select distinct subject_code from B;
Now i want the names from A of all the subject whose codes i got from B.
use inner join
SELECT A.*
FROM A inner join B on A.subject_code =B.subject_code
where B.Roll_number = //here enter roll number
Based on understanding of your limited problem description, a simple INNER JOIN is solution to your problem.
Try the following query ( $input_roll_number is your input value for a specific roll number, whose subject name you want to determine ):
SELECT A.roll_number, B.subject_name
FROM A
INNER JOIN B ON A.subject_code = B.subject_code
WHERE A.roll_number = $input_roll_number
Related
This question already has answers here:
Joining three tables using MySQL
(11 answers)
Closed last year.
I got 4 tables: galleries, areas, stores and gallery_element.
So I need to create a big query in which I could join the data(ex. name column) from galleries, areas, stores which are connected throug gallery_element table.
gallery_element has: id, gallery_id, area_id, store_id columns.
Can someone give me an simple example so I can practice with it further by myself.
Thanks!
I think it can help you to start your query:
select gallery_element.id, galleries.name, areas.name, stores.name from gallery_element
inner join galleries on galleries.id = gallery_element.galleries_id
inner join areas on areas.id = gallery_element.area_id
inner join stores on stores.id = gallery_element.store_id
This question already has answers here:
Joining on columns of different type?
(2 answers)
Closed 3 years ago.
I have 2 tables I would like to left join together.
Table A has my reference data with ID of type integer.
Table B stores my data and one of the columns has a loose link to my reference table. This means it can hold the ID from the other table or some arbitrary text.
I would like to perform a query joining them together and displaying the data from reference table only if a record with matching ID is found.
A much simplified example (MCVE):
CREATE TABLE a
(
id INT
);
INSERT INTO a VALUES (5);
CREATE TABLE b
(
id VARCHAR(10)
);
INSERT INTO b VALUES ('5sdf');
I tried this query:
SELECT * FROM a LEFT JOIN b USING(id)
but the result is 5, which is not what I expected because values are different - http://www.sqlfiddle.com/#!9/6c5965/1
I also tried with explicit ON clause:
SELECT * FROM a LEFT JOIN b ON a.id=b.id
but this one was also joined. - http://www.sqlfiddle.com/#!9/6c5965/2/0
How can I join the two tables with columns of different data types without implicit casting?
Convert the integer to a string when joining, rather than relying on implicit conversions, which default the other way.
SELECT * FROM a LEFT JOIN b ON CAST(a.id AS CHAR) = b.id;
fiddle
This question already has an answer here:
SELECT .. INTO returns more than one rows - Mysql
(1 answer)
Closed 4 years ago.
I have a query like this to select the course that the student is studying:
SELECT course_student.course from course_student inner join
(select student.student_id from student inner join reg_user
on student.reg_user = reg_user.reg_user_id
WHERE reg_user_id=5) as tmp
on tmp.student_id = course_student.student into #vcourse;
However, as reg_user_id=5 will returns 2 rows because this student takes 2 courses, the following query which return all the students in the same course as user 5, returns error "Result consisted more than one row".
select family_name, middle_name, given_name FROM reg_user
inner join (select reg_user from lecturer inner join course_lecturer
on lecturer = lecturer_id
where course = #vcourse) as temp on temp.reg_user = reg_user_id;
I know this problem is due to using SELECT...INTO query but I don't know how to solve it. If anyone know how put more than one results into the WHERE condition, I'm really appreciated. Thank you
Ps: Here is the database structure so that you can follow my queries
I want to get all the result from the first query and using those result in the second one.
The first one will select all the course_id for student_id=5.
The second one will select all the name of the register users who are students and in the same course as the student with id=5.
For your problem, I see several solutions:
either review the query so that it returns only one line. For example by adding a LIMIT clause 1
either store the result in a temporary table instead of a variable
or work with CURSOR to be able to process each line returned by the request in the host program
This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 5 years ago.
since 2 days I'm trying to find a solution...
I have two tables:
-- components -- colums:
id | name | description
-- components_ingredients -- colums:
component_id | ingredient_id
=> one component can have multiple ingredients
so when I join the tables with my statement:
SELECT * FROM components c
INNER JOIN components_ingredients ci ON c.id = ci.component_id
I get back one row for every ingredient in table ci. But I want to get back only one row with the matched ingredients as additional columns like:
c.id | c.name | c.description | ci.ingredient1 | ci.ingredient2 | ci.ingredient3 ...
Is this possible and when how??
Thanks
You can try using MySQL's GROUP_CONCAT() function to create a CSV list of the ingredients for each given component.
SELECT c.id, c.name, c.description, ci.ingredients
FROM components c
INNER JOIN
(
SELECT component_id, GROUP_CONCAT(ingredient_id) AS ingredients
FROM components_ingredients
GROUP BY component_id
) ci
ON c.id = ci.component_id
Note that as #Gordon pointed out, you might be able to do without the subquery I used, but in general you might need it. The reason Gordon's query works, even according to the ANSI standard, is a given id in the components table should uniquely determine the name and description. Hence, it is OK to include those columns while using GROUP BY, because there is no ambiguity involved.
It is hard to put the ingredients in separate columns, because you don't now how many there are.
Much easier is to concatenate them together into a string in one column:
SELECT c.*, GROUP_CONCAT(ci.component_id) as component_ids
FROM components c INNER JOIN
components_ingredients ci
ON c.id = ci.component_id
GROUP BY c.id;
Note: It is generally bad practice to include columns in the SELECT that are not in the GROUP BY. However, it is okay in this case, because components.id uniquely identifies each row. This functionality is even specified as okay in the ANSI standard -- although few databases actually implement it.
This question already has answers here:
Joining two tables in a MySQL
(2 answers)
Closed 8 years ago.
I want to select all from a table called Customer where CustomerID = 'AC001' and I also want to join in another table Pets where the CustomerID(fk) is the same as the CustomerID in Customer table is that possible?
Customer table
CustomerID,TypeOfCustomer, FName,Adress,City,State,Zipcode,PhoneNr,FaxNr
Pets table
PetID,FName, Animal,Breed,Gender,DoB,CustomerID(fk)
This is a very basic SQL question, here you go: to join the two tables together, do this:
SELECT *
FROM Customer
JOIN Pets ON Pets.CustomerId=Customer.CustomerID
To filter for CustomerID = 'AC001', add a WHERE clause.
Note also that the result won't contain customers without any pets. If you want to include those as well, do a LEFT JOIN instead.
Generally, I'd recommend reading a good beginner-level book on SQL and relational databases to make sure you grasp the underlying concepts here.
Select //fields you want// from Customer C, Pets P
WHERE C.CustomerID=P.CustomerID