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';
Related
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!
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
I have 3 tables,
itemmaster
|--------|----------|
| id | name |
|--------|----------|
| 1 | Pizza |
|--------|----------|
| 2 | Burger |
|--------|----------|
| 3 | Pepsi |
---------------------
order
|--------|----------|
|orderid | date |
|--------|----------|
| 1 | 1-1-11 |
|--------|----------|
| 2 | 2-1-11 |
|--------|----------|
| 3 | 3-1-11 |
---------------------
orderdetails
|--------|-------------|---------|---------|
| id | orderid |itemid |quantity |
|--------|-------------|---------|---------|
| 1 | 1 | 1 | 10 |
|--------|-------------|---------|---------|
| 2 | 1 | 2 | 20 |
|--------|-------------|---------|---------|
| 3 | 2 | 1 | 10 |
-------------------------------------------
I want to join these 3 tables to get quantity of items of an order that placed on a particular date.
What I have tried is
$this->db->from('itemmaster');
$this->db->join('orderdetails', 'orderdetails.itemid= itemmaster.id','left');
$this->db->join('order', 'order.orderid= orderdetails.orderid');
$this->db->where('order.date',"1-1-11");
$query = $this->db->get();
I got Result as,
Pizza------ 10
Burger------10
What I want is,
Pizza-------10
Burger------20
Pepsi-------0
If changing the all the joins to left joins, you can always do it in two separate query and do a union between the two of them. One would get all the lines that actualy have a quantity and the other would get the rests and put them toguether.
It would look a bit like this. there might be some synthax error, but you'll have to rewrite it in php anyway:
p.s. to add up the quantities, you can use sum()
Select itemmaster.name, orderdetails.quantity from itemmaster
left join orderdetails on orderdetails.itemid = itemmaster.id
left join order on order.orderid = orderdetails.orderid
where order.date = '1-1-11'
group by itemmaster.name
Union
Select itemmaster.name, '0' as quantity From itemmaster
except (Select itemmaster.name, orderdetails.quantity from itemmaster
left join orderdetails on orderdetails.itemid = itemmaster.id
left join order on order.orderid = orderdetails.orderid
where order.date = '1-1-11'
group by itemmaster.name)
group by itemmaster.name
hope this helps ! good luck
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;
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?>