David
David

Reputation: 6913

mysql validating data from multiple tables data

Edited

I realized that I did not included the brand code ID now master table holds the brand ID where only 1001 and 1002 are the ones that I care about the other brands been third party brands.

end edited part

first thanks to all the community for the help provided in here, is been very useful all the time. I am a completely noob at mysql, I develop java, javascript and currently php, in my current project I face an issue where I need to retrieve data from our database, considering that I cannot modify in any way none of the databases the problem is as follow:

Having a shopping site that sells different brands including two own brands for the example lets name them "puma", "nike", "ownbrand1" and "ownbrand2", the warehouse only keep stock of "ownbrand1" and "ownbrand2", if a customer buy any of the other brands the third company will send the requested product as on need basis. The website offers the choice of filtering results in many was, one of which is by showing only items that we currently have on stock at the moment.

Say we have 3 tables, the first table, lets call it "stock" have the stock amount with internal unique code for each product (own brands only) the second "master" have the information of every single product with unique code id and general product id and the third table "photo" hold the pictures for the purpose of showing on the website holding the picture information and the general product id.

The products are identified by unique product code id different for each product that means that the same product but different size or color will have different code id, the second id general product id is different only for each product, that means that different size or color wil have the same product id and the picture information that is the picture of one of the products in general.

------------------------------ 
          Stock 
------------------------------ 
ID            Name         Stock 
00001     puma1             0 
00002     puma2             0 
00003     ownbrand1(s)      0 
00004     ownbrand2(l)      0 
00005     nike              0 
00006     ownbrand1(l)      1 
00007     ownbrand1(m)      3 

---------------------------------------------
        Master 
---------------------------------------------
 ID         Name         GeneralId    BrandId
00001   puma1             pum001      1030
00002   puma2             pum001      1030
00003   ownbrand1(s)      owbr001     1001
00004   ownbrand2(l)      owbr002     1002
00005   nike              nike001     1040
00006   ownbrand1(l)      owbr001     1001
00007   ownbrand1(m)      owbr001     1001

------------------------------ 
        Photo 

------------------------------ 
GeneralId     Picture
pum001      pum001.jpg
owbr001     owbr001.jpg
owbr002    owbr002.jpg
nike001     nike001.jpg

If the option for only stock items is clicked the page will show the items from third party and only the ones with stock gratter than 0 from ownbrand in this case will show

Desired result

"puma" = show
"nike" = show
"ownbrand1" = show (because it have stock in some sizes)
"ownbrand2" = not shown (because there is no stock)

Right now I accomplish the task doing first this query:

$sql_query = "SELECT stock.*, master.*  FROM stock, master WHERE master.ID = stock.ID AND stock.stock > '0'"

Then generate an array with the generalID code of the items on stock taking it from the master table, and then generate the follow query:

$sql_query = "SELECT * FROM master WHERE generalID NOT IN $previous_array"

and join the two arrays to generate the whole list of products brand to show doing this last query after some more filtering for categorizing:

$sql_query = "SELECT * FROM photo WHERE generalID IN $complete_array" 

Now this method is giving me the result I need but as you can expect with over 2000 items and 30+ brands it does compromise the performance speed a bit, I tried doing UNION query but I really could not get my head around the idea.

If any of you sql masters could help me understand this I would be a very happy bunny.

Upvotes: 0

Views: 1637

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 2915

SELECT *
FROM 
 (SELECT * 
 FROM stock 
   inner join master using (ID) 
   inner join photo using (generalID)
 WHERE (master.BrandId!=1001 and master.BrandId!=1002) or stock.stock>0
 ) as whatever
GROUP BY master.generalID

Upvotes: 1

Related Questions