简体   繁体   English

mysql验证来自多个表数据的数据

[英]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. 我意识到我没有包括品牌代码ID,现在主表保存品牌ID,其中我只关心1001和1002的其他品牌是第三方品牌。

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: 我对mysql完全陌生,我开发java,javascript和当前的php,在我当前的项目中,我面临一个问题,我需要从数据库中检索数据,考虑到我无法以任何方式修改任何数据库,问题是如下:

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. 假设有一个购物网站出售不同的品牌,例如两个自有品牌,则将其命名为“ puma”,“ nike”,“ ownbrand1”和“ ownbrand2”,而仓库仅保留“ ownbrand1”和“ ownbrand2”的库存客户购买其他品牌的商品时,第三公司将根据需要发送所需的产品。 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. 假设我们有3个表,第一个表,称其为“库存”,具有每种产品的内部唯一代码的存货量(仅限自有品牌),第二个“主”具有每个产品的信息,其唯一代码ID和常规产品ID和第三个表格“照片”用于保存图片,以便在网站上显示包含图片信息和一般产品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. 产品通过唯一的产品代码ID进行标识,每个产品ID均不同,这意味着相同的产品但尺寸或颜色不同的产品将具有不同的代码ID,第二个ID常规产品ID仅针对每个产品都不同,这意味着尺寸或颜色不同具有相同的产品ID和通常是其中一种产品图片的图片信息。

------------------------------ 
          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 如果单击仅库存项目的选项,则页面将显示来自第三方的项目,在这种情况下,仅显示自身品牌的库存大于0的项目

Desired result 所需结果

"puma" = show “美洲狮” =显示
"nike" = show “ nike” =显示
"ownbrand1" = show (because it have stock in some sizes) “ ownbrand1” =显示(因为它有一些尺寸的库存)
"ownbrand2" = not shown (because there is no stock) “ ownbrand2” =未显示(因为没有库存)

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: 然后生成一个数组,其中包含要从主表中获取的库存商品的generalID代码,然后生成以下查询:

$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. 现在,这种方法给了我所需的结果,但是正如您可以预期的那样,在超过2000种商品和30多个品牌的情况下,它确实在一定程度上降低了性能速度,我尝试进行UNION查询,但我真的无法理解。

If any of you sql masters could help me understand this I would be a very happy bunny. 如果你们中的任何sql大师都可以帮助我理解这一点,我将是一个非常高兴的兔子。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM