Display categories being supplied by a supplier - php

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!

Related

JOIN 2 tables based by multiple ids mysql

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 |

Multiple joins in MySQL table

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

How to select values from two different tables in SQL

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';

Select records without category in many to many relationship

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;

Join SQL the book_list with recursive my book_category with PHP MYSQL

I have two tables names book_list and book_category. Schema is like below:
book_list
+---------+--------+-----------+
| id_book | id_cat | book_name |
+---------+--------+-----------+
| 1 | 3 | Book Nam1 |
| 2 | 1 | Book Nam2 |
| 3 | 2 | Book Nam3 |
+---------+--------+-----------+
book_category
+--------+----------+-----------+-------+
| id_cat | cat_name | id_parent | level |
+--------+----------+-----------+-------+
| 1 | name1 | 0 | 1 |
| 2 | name1.1 | 1 | 2 |
| 3 |name1.1.1 | 2 | 3 |
+--------+----------+-----------+-------+
code tried:
SELECT a.id_book, a.id_cat, a.book_name, b.cat_name, b.level
FROM book_list a
INNER JOIN book_category b ON a.id_cat = b.id_cat
I need to manipulate the result array to add breadcrumb where each has link to itselfs like:
table row:
<td>3(id_book)</td>
<td>Book Nam3 (book_name)</td>
<td>
Name 1 »
Name 1.1 »
Name 1.1.1
</td>
Hope I made my point and thank you for any help.
Try this query.
SELECT
bl.id_book, bl.id_cat, bl.book_name, tc2.cat_name, tc2.level
FROM
book_list bl
JOIN book_category tc
ON tc.id_cat = bl.id_cat
JOIN book_category tc2
ON LOCATE(tc2.cat_name, tc.cat_name) > 0
WHERE
tc.id_cat = 3
This query is based on occurrence of cat_name substrings.
TRY
SELECT a.id_book, a.id_cat, a.book_name , b.cat_name, b.level
FROM book_list a
JOIN book_category b ON USING(id_cat)
and then
<?=$cat_name?>

Categories