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
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:
MySQL Join two tables and combine multiple rows into one
(2 answers)
Closed 1 year ago.
I'm having trouble figuring out how to write an SQL statement...
Let's say I have two tables:
Projects
and Tasks
I'm trying to come up with an SQL statement that returns a unique list of projects and their associated tasks grouped together by commas like this...
If I need to create a separate table (e.g. Projects_Tasks) for a many-to-many relationship, I'm fine with that, but I'm still not sure what SQL statements would generate that final table.
SELECT t.ProjectId, p.Name, t.Tasks
FROM Projects p
JOIN (
SELECT ProjectId,GROUP_CONCAT(Name) as Tasks
FROM Tasks
GROUP BY ProjectId) as t
ON t.ProjectId = p.ProjectId;
You need to JOIN the Tasks table to the Projects table, then group the results by Project ID. This allows you to use GROUP_CONCAT() to concatenate the task names
SELECT Projects.Name, group_concat(Tasks.Name separator ', ')
FROM Projects
join Tasks
on Projects.id = Tasks.projectId
group by Tasks.projectId
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
This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 5 years ago.
Order Table
Product Table
I need to select all from these two tables. Is it possible?
With the following query you can select all data from one table:
SELECT * FROM Order;
If you want to select all data from two tables you can use a join in the select query to connect the data of the two tables:
SELECT * FROM Order
JOIN Product ON Order.p_id = Product.p_id;
There are different kinds of joins available. Based on the one you use can change the amount of data you will receive. The different kinds of joins are:
(Inner) join: Returns records that have matching values in both tables
Left (Outer) join: Return all records from the left table, and the matched records from the right table
Right (outer) join: Return all records from the right table, and the matched records from the left table
Full (outer) join: Return all records when there is a match in either left or right table
The information about the different kinds of joins comes from the following website:https://www.w3schools.com/sql/sql_join.asp
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.