Reputation: 314
I have a problem in mysql. I need to fetch certain sets of records from a table that has the following data.
Let me brief you guys about the table. every primary item has a similar item and that similar item has a size.
but we have to show unique items from the above table having least size.
for example.. primary item is A and it's corresponding item in X and X is also primary and it's similar item in A but X also has a similar item P so A,X,P all are similar and in these the item with least size is P with size 4 so we have fetch P from the table with a query.
Furthermore the records are 1-M and all primary item as similar items and all similar items exits in the primary field in the database. And this table contain nearly 3 lacs record for fetching and looping one records at a time will not work..
Any help will be appreciate.
Thanks Jawed Shamshedi
Upvotes: 0
Views: 100
Reputation: 14500
Given the schema:
CREATE TABLE similar_test ( id int(11) NOT NULL DEFAULT '0',
Stock varchar(20) NOT NULL, SimilarStock varchar(20) NOT NULL,
ItemSize decimal(18,2) DEFAULT '0.00' ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
please check this : I am considering for 1 level depth :
select * , s.ItemSize as minSize from similar_test as p
join similar_test a s on s.Stock= p.SimilarStock
where p.Sock='A'
order by s.itemSize
limit 1
If that is not addressing you questions as you said in comments the other options are :
a. create another table which will hold PRE- calculated values are desired
b. Write a stored procedure to go through the item list upto N-levels as N is unspecified in this case (as I suspected)
c. Do the sorting and grouping in PHP/or any other language you are using
this might also help you hierarchical data
regards
Upvotes: 1