If I have one table that references the names of sponsors and the product ids of the products they recommend, as such:
--------------------------------
|Name |Product ID 1|Product ID 2|
--------------------------------
|Jon | 1 | 3 |
|Sally| 1 | 2 |
--------------------------------
And another table that lists the products:
----------------------------------------
|Product ID |Product Name|Product Price|
----------------------------------------
| 1 | Prod 1 | 25 |
| 2 | Prod 2 | 35 |
| 3 | Prod 3 | 45 |
----------------------------------------
How do I join these together so that I have the name of sponsor plus each product name and product price that they recommend? INNER JOIN and LEFT JOIN only seem to pull through one of the products, but not all of them.
Join twice.
SELECT s.name, p1.ProductName AS product_1_name, p1.ProductPrice AS product_1_price, p2.ProductName AS product_2_name, p2.ProductPrice AS product_2_price
FROM sponsers AS s
JOIN products AS p1 ON s.ProductID1 = p1.ProductID
JOIN products AS p2 ON s.ProductID2 = p2.ProductID
Related
I have 2 tables let's say orders
id | memberid | productsid |
----------------------------
1 | 23 | 25,27
and products
id | product_name | price |
----------------------------
25 | product1 | 120
27 | product2 | 50
I want to join orders and products table to get product name and price for each id from productsid.
This is the way how I tried to store an order for a member. If you have any better solution, I am waiting to know.
You can join the tables with the help of the function find_in_set(), then group by each order and with group_concat() create a list of the products:
select o.id, o.memberid,
group_concat(p.product_name order by find_in_set(p.id, o.productsid)) products
from orders o inner join products p
on find_in_set(p.id, o.productsid)
group by o.id, o.memberid
See the demo.
Results:
| id | memberid | products |
| --- | -------- | ----------------- |
| 1 | 23 | product1,product2 |
I have my 2 tables : tbl_supplier & tbl_categories
I want to display all the categories being supplied by a supplier in a dropdown select option in php html
Example:
Supplier1 > Category1, Category2, Category3
Supplier2 > Category2, Category4
You need a third table with one row per Supplier/Category combination:
Supplier
| id | name |
|----|------|
| 1 | John |
| 2 | Ana |
| 3 | Dan |
Category
| id | name |
|----|------------|
| 1 | category 1 |
| 2 | category 2 |
| 3 | category 3 |
Supplier_categories
| id | Supplier_id | categ_id |
|----|-------------|----------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
| 4 | 2 | 3 |
Then you can retrieve a list of each supplier categories by doing INNER JOIN to the three tables and putting conditions like this:
SELECT Supplier.id, Category.name
FROM Supplier
INNER JOIN Supplier_categories
on Supplier_categories.Supplier_id = Supplier.id
INNER JOIN Category
ON Category.id = Supplier_categories.categ_id
WHERE Supplier.id = 1 //(for example)
Assuming your tbl_supplier contains a sid primary key, and then that key is present in tbl_categories to keep track of suppliers, and cat is the column to hold category name, the query would be like this:
Select c.cat from tbl_supplier as t, tbl_categories as c where t.sid=c.sid and t.sid='supplier-1'
Example for Supplier 1:
<label>Supplier1:</label>
<select name="supplier-1">
<option>All</option>
<?php
$query = mysqli_query($conn,"Select c.cat from tbl_supplier as t, tbl_categories as c where t.sid=c.sid and t.sid='supplier-1'");
while($row=mysqli_fetch_array($query)){
echo "<option>".$row['cat']."</option>";
}
?>
</select>
However, if you would like to dynamically add multiple suppliers dropdowns then you would need to write a more complex query.
Hope I helped!
I have three tables (MySQL):
families where I define the products' families
products where I define the products
families_products where I relate families and products
------------------- -------------------- ------------------------
| familyID | code | | productID | code | | familyID | productID |
|----------|------| |-----------|------| |----------|-----------|
| 1 | p | | 1 | p3 | | 1 | 1 |
| 2 | a | | 2 | a5 | | 1 | 3 |
| 3 | e | | 3 | p1 | | 1 | 6 |
------------------- | 4 | e7 | | 2 | 2 |
| 5 | a2 | | 2 | 5 |
| 6 | p4 | | 3 | 4 |
-------------------- ------------------------
I have two questions:
Is this design convenient or is it better drop the families_products table putting the familyID relation directly into the table products?
With a design like this one, if I have the familyID how can I retrieve the products->code? I wrote this query but a query structure like this one would work if I drop the families_products table putting the familyID relation directly into the table products as said before, not in the case of a third relational table.
'SELECT productID, code, img
FROM products AS a
INNER JOIN families_products AS b
ON b.productID=a.productID
WHERE b.familyID=' . $families[$key]["familyID"]
Your data structure is fine.
If you have at most one familyID per product, then you should put the family in the products table. You should use the junction table (your structure) if a product can be part of multiple families.
As for your query, it is fine. I would use better table aliases:
SELECT p.productID, p.code, ??.img
FROM products p INNER JOIN
families_products fp
ON f.productID = fp.productID
WHERE fp.familyID = ' . $families[$key]["familyID"]
If your product only belongs to one product family you can skip the "familiy_product" table and add the familiy_id directly to the product. Only in case a product can be assigned to multipl families you need the "join" table.
With only two tables the SQL is quite easy:
select p.code from family f, product p where f.code ='xx' and p.family_id = f.id
I have two tables in my SQL Server database. The first is catgeories and second is products. There is a column categories_id in both tables.
HERE IS MY TABLE FOR CATEGORIES :
+----+----------+----------+
| id | category | parent |
+----+----------+----------+
| 1 | BOY | 0 |
| 2 | GIRL | 0 |
| 3 | SHIRT | 1 |
| 4 | SKIRT | 2 |
| 5 | JACKET | 1 |
+----+----------+----------+
TABLE : PRODUCTS
+-------+--------------+----------------------+
| id | title |PRICE | Categories |
+-------+--------------+------+---------------+
| 1 | RED SHIRT | 300 | 3 |
| 2 | blue SKIRT | 500 | 4 |
| 3 | jeans jacket | 500 | 3 |
+-------+--------------+------+-----+---------+
Now I want to select the values from Products table for a particular category like BOY.
Try This:
SELECT pr.id,pr.title,pr.price from products AS pr
INNER JOIN CATEGORIES AS cat ON cat.id=pr.Categories
WHERE cat.category='Boy';
SELECT products.* FROM products INNER JOIN categories ON products.categories = categories.id WHERE categories.category LIKE '%BOY%';
Use this query..
Either
SELECT * FROM tproducts WHERE categories = 1
or
SELECT * FROM tproducts
JOIN tcategories ON tcategories.id = tproducts.categories WHERE tcategories.category = 'BOY'
I don't know your table names so I just used tproducts and tcategories
There is no boy(1) ID in Categories(field) in PRODUCTS(table)
SELECT * FROM CATEGORIES
INNER JOIN PRODUCTS
ON CATEGORIES.id=PRODUCTS.Categories
WHERE CATEGORIES.category ='BOY'
FOR this use join query Example:
select *(or you can get any column as you write name) from table1 join table2 on table1.id=table2.id where table1.category='BOY';
I have a db with some products, they can be "tagged" with many categories. Some products have not an assigned category and I need to find them. Ineed to find records without category. I have created this tables:
ITEMS
+----+------+
| ID | Item |
+----+------+
| 1 | qwe |
| 2 | asw |
| 3 | wgr |
+----+------+
CATEGORIES
+----+----------+
| ID | Category |
+----+----------+
| 1 | xxx |
| 2 | yyy |
+----+----------+
RELATIONSHIP
+---------+--------+
| id_item | id_cat |
+---------+--------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
+---------+--------+
This is the (not working) query:
SELECT item.id COUNT(relationship.id_item) AS n
FROM item
JOIN relationships GROUP
BY relationships .n
WHERE item.id = relationships.id_item
AND relationships.n =0;
you can use subquery with NOT IN clause
SELECT i.id
FROM item i
WHERE i.id NOT IN ( SELECT r.id_item
FROM RELATIONSHIP r);
You want a left join and then find the ones that don't match:
SELECT i.id, COUNT(relationship.id_item) AS n
FROM item i LEFT OUTER JOIN
relationships r
on i.id = r.id_item
WHERE r.id_item is null;