Mysql query to retrive data from join of three table [duplicate] - mysql

This question already has answers here:
MySQL pivot row into dynamic number of columns
(1 answer)
MySQL - Rows to Columns
(13 answers)
Closed 5 years ago.
I have tree tables:
Persons:
id
name
Books
id
title
Quantity:
People_id
Product_id
quantity
I need a result with :
in the columns the Book title, in the rows the Persons name, in the cells the quantity take from the cross of peoples and books

select *
from persons p join quantity q on p.id = q.people_id
join books b on q.product_id = b.id

JOIN should help (if you need columns from all table then add alias.* for other tables in SELECT).
SELECT p.*
FROM persons p
JOIN quantity q ON p.id = q.people_id
JOIN books b ON q.product_id = b.id

Related

SQL: Combine results of queries [duplicate]

This question already has answers here:
How to join two tables mysql?
(4 answers)
Closed 1 year ago.
I have 2 tables as such:
StudentID
Email
1
him#school.edu
2
her#school.edu
Book Title
Borrowed By (Student ID)
Borrowed Date
Some Book
13
10/10/2021
Other Book
456
10/15/2021
I want to make a single SQL query to return the email of the student and borrowed date for a specific book title.
Something like
SELECT
*
FROM
student_table
WHERE
StudentID = (SELECT
StudentID
FROM
borrowed_table
WHERE
BookTitle = 'Some Book');
but this would only return the Email. I want to return the borrowed date as well.
How can I combine the results of both queries?
Can this be done without specifying the columns (UNION of *) ?
Join both tables
SELECT
st.*,bt.`Borrowed Date`
FROM
student_table st
INNER JOIN borrowed_table bt ON st.StudentID = bt.StudentID
WHERE
bt.BookTitle = 'Some Book';
Select st.BookTitle, st.BorrowedBy, bt.BorrowedDate
from Student_table st inner join
borrowed_table bt on st.StudentID = bt.StudentID
where bt.BookTitle = 'Some book title'

Select in M-N relationship [duplicate]

This question already has answers here:
Inner Join Many to Many Tables with Filter In MySQL
(3 answers)
Closed 2 years ago.
I have the following tables:
product{
id
name
}
product_keyword{
prod_id
keyword_id
}
keyword{
id
keyword
}
Where a given product can have multiple keywords and a keyword can belong to multiple products.
Now I would like to, given a certain product, select all the "keyword-related" products. That is, if "prod" have keywords "key1" and "key2" I would like to select all products that have either "key1" or "key2" as keywords.
You can do this with joins:
select distinct p1.id, p1.name
from product p1
inner join product_keyword pk1 on pk1.prod_id = p1.id
inner join product_keyword pk2 on pk2.keyword_id = pk1.keyword_id
inner join product p2 on p2.id = pk2.prod_id
where p2.name = ?
The question mark represents the name of the product whose associated products you want to bring.

How do I apply filters from multiple rows to a joined table in MySQL? [duplicate]

This question already has answers here:
SELECTING with multiple WHERE conditions on same column
(12 answers)
Closed 2 years ago.
I am working on a project where I'd like to display products that are subject to certain filters. Let's say I have the following 2 tables:
Products
id name
--------------
1 'Product 1'
2 'Product 2'
Filters
product_id filter_id
----------------------
1 a
1 b
2 a
I'm trying to write a query that only returns products if the filters are set. So if filter 'a' is active the results should be product 1 and 2, and if 'a' AND 'b' are active it should ONLY return product 1.
I've tried:
SELECT p.id
FROM products p
LEFT JOIN filters f ON f.product_id = p.id
WHERE filter.id = 'a'
GROUP BY p.id
This returns the id's for product 1 and 2 as expected. However, when I try:
SELECT p.id
FROM products p
LEFT JOIN filters f ON f.product_id = p.id
WHERE filter.id = 'a' AND filter.id = 'b'
GROUP BY p.id
I'd expect it to return the id for product 1, but it returns no results. How can I rewrite my query so that I get the product id's for the active filters? Can this be done with MySQL alone or do I have to loop through the results with php?
Your query just needs a slight change: Use OR instead of AND...change it to this:
SELECT p.id
FROM products p
LEFT JOIN filters f ON f.product_id = p.id
WHERE filter_id in ('a','b')
GROUP BY p.id
HAVING COUNT(*) = no_of_filters

count(distinct(col_name)) not working in my sql [duplicate]

This question already has answers here:
Query using group_concat is returning only one row
(4 answers)
Closed 2 years ago.
I am newbie in mysql, for learning i am using sample database of w3schools. in that i want to create a view which will for each supplier, list the supplier name, the count of distinct products by the supplier (PCount), the count of distinct product categories by the supplier (CCount).
here is my table structures
Suppliers Products
-------------------- ----------------------------
SupplierID ProductID
SupplierName ProductName
ContactNumber SupplierID
Address CategoryID
City Unit
PostalCode Price
Country
Phone
What i have try is
SELECT s.SupplierName, COUNT(DISTINCT(p.ProductID)) as PCount, COUNT(DISTINCT(p.CategoryID)) as CCount FROM Suppliers as s
right join Products as p
on s.SupplierID = p.SupplierID
this is only giving me single SupplierName not all the supplier details with distinct product and distinct category.
Any help would be much appreciated.
Give this a shot:
SELECT s.SupplierName,
COUNT(p1.ProductID) AS PCount,
COUNT(DISTINCT p2.CategoryID) AS CCount
FROM Suppliers s
LEFT JOIN Products p1 ON s.SupplierID=p1.SupplierID
LEFT JOIN Products p2 ON s.SupplierID=p2.SupplierID
GROUP BY s.SupplierName
ORDER BY s.SupplierName

Join 2 column same table with another column [duplicate]

This question already has answers here:
How to join two columns to the same table [duplicate]
(3 answers)
Closed 5 years ago.
I want to join 2 column that share same foreign key in another table
here's the tables:
country:
idcountry| countryname
1 german
2 america
destination
id|fromcountry |tocountry
1 1 2
the result that i wanted to:
id|fromc |toc
1 german america
Use simple Left Join ans provide alias to country name column:
SELECT d.idcountry as id, cf.CountryName as fromc, ct.CountryName as toc
FROM destination d
LEFT JOIN country cf ON d.fromcountry = cf.idcountry
LEFT JOIN country ct ON d.tocountry = ct.idcountry
Use Left Outer Join for both fields
something like this
SELECT Dest.ID, CFrom.CountryName, CTo.CountryName
FROM Destination Dest
LEFT OUTER JOIN Country CFrom ON Dest.FromCountry = CFrom.idcountry
LEFT OUTER JOIN Country CTo ON Dest.ToCountry = CTo.idcountry