**CategoryMaster**
CategoryId Category Name
1 ABC
2 Xyz
3 PQR
**Product master**
ProductId Name
1 ABCD
2 WXYZ
3 UVWX
**ProductCategory**
ProCatID ProductId CategoryId
1 1 1
2 1 2
3 2 3
4 3 2
5 3 3
**ProductDetail**
ProductDetailId Price Qty Date ProductId
1 250 8 2-11-2011 1
2 200 10 2-12-2011 2
I want to featch all product detail from product master table with its category my table structure is same as above
Your ProductMaster table is not normalized. This is not good: you should model like this:
Productmaster
- productid
- name
ProductCategories:
- productid
- categoryID
Related
I have table in database like:
===========================
products_id | categories_id
===========================
1 2
1 1
1 3
1 4
1 5
2 5
2 2
2 3
2 4
3 5
4 5
5 5
===========================
I'm not so good in mysql so i need some help with this query. I want to list all products_id that have only categories_id = 5. That is, even if the categories_id is 5, it should only be listed if no other row exists with that products_id and a different categories_id.
So the output should be : 3 4 5.
Here is one approach using aggregation and filtering with a having clause:
select product_id
from mytable
group by product_id
having min(category_id) = 5 and max(category_id) = min(category_id)
I have 3 tables product, version and Currency.
Is it possible to query products that have the MAX version but has not yet have any currency where currency version_id = the id of the MAX version?
EDIT
Requested to have example data.
product table
id name
1 product a
2 product b
3 product c
4 product d
version table
id product_id version_no
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
6 4 1
currency table
id product_id version_id currency
1 1 1 USD
2 2 2 USD
3 2 3 USD
4 3 4 USD
From this data, I should get this returned:
product_id product_name
3 product c
4 product d
This is because on Max versions of the following products
product_id version
1 1
2 2
3 2
4 1
Only product 3 and 4 have no entries on the currency table with these version_nos
Is this clear enough?
Use left join: and the where condition with null value will give you your desired result
select product_id,name
from
(select product_id, max(version_id) as vid
from versiontable
group by product_id) v
left join currencytable c on v.product_id=c.product_id and v.vid=c.version_id
left join procuttable p on p.id=v.product_id
where c.product_id is null and c.version_id is null
persons table
-----------------
id name fatherid motherid famid
---------------------
1 ras lovely beautiful 5
2 abc def ghi 5
3 xyz abc aay 4
family table
----------------
famid hof ( id from persons)
------------------------
4 3
5 2
output required
-------------------
membercount(id with famid) hof_name hof_id(id from person) famid
-------------------
2 abc 2 5
1 xyz 3 4
please help me to get the correct query
If I have a "SALES" table, with columns of SaleID, Product#, and CustomerName. and a PRODUCTS table with two columns product_ID and Name. The contains 5 differnt products. In the SALES table populates when a sale is made.
How would I query customer_name with only Product_ID of 1 and 2?
sales table
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 DAVE
2 2 DAVE
3 3 DAVE
4 1 TOM
5 2 TOM
6 1 JANE
7 1 MIKE
8 1 MIKE
9 3 MIKE
10 4 MARY
I would like a table result to be
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 TOM
2 2 TOM
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =1
INTERSECT
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =2
I have the following tables:
customers
id name
1 customer 1
stores
id name customer_id
1 store 1 1
2 store 2 1
3 store 3 1
items
id name store_id customer_id inventory
1 item 1 1 1 10
2 item 2 2 1 12
3 item 3 2 1 18
item_attributes
id item_id store_id customer_id inventory
1 1 2 1 4
2 1 3 1 3
3 2 1 1 7
4 2 3 1 0
5 3 1 1 9
What I want is this:
item ID Item Name store 1 Store 2 Store 3
1 Item 1 10 4 3
2 Item 2 7 12 0
3 Item 3 9 18 0
A customer can have many stores.
Each store can have many items.
Other stores may or may not have the entry in the item_attributes table.
I want to get an consolidated report on item inventory.
My approach is:
Get all the items for the customer 1.
Get all the stores for the customer 1.
Get all records from the item_attributes.
foreach($i=0; $i<count($items); $i++){
foreach($j=0; $j<count($stores); $j++){
// now I get the item_id and store_id.
// check if exists in the $item_attr array with this combination.
// return the inventory if yes, 0 otherwise.
}
}
Is there any easier approach than the one I mentioned above? Any thoughts would appreciated.