简体   繁体   English

mysql join将值或空值添加到列中

[英]mysql join getting a value or a null into a column

I am very new to SQL, so I am having a couple problems figuring things out. 我对SQL非常陌生,因此在解决问题时遇到了一些问题。 The database is for an online store, and the structure is something like this: 该数据库用于在线商店,其结构如下所示:

users: UserID, UserName, etc. 用户:UserID,UserName等。

pricelists: PricelistID, UserID, ProductItemID, Price 价目表:PricelistID,UserID,ProductItemID,Price

productitems: ProductItemID, ProductID, ItemID productitems:ProductItemID,ProductID,ItemID

products: ProductID, ManufacturerID, WebPageText, etc. 产品:ProductID,ManufacturerID,WebPageText等。

Each product can have one or more items (eg if the product page is selling T-shirts, there may be 5 different T-shirt variations for sale). 每个产品可以有一个或多个项目(例如,如果产品页面上出售T恤,则可能有5种不同的T恤出售)。 That's pretty normal. 那很正常。 What's not normal is that the pricing for each item is determined by what's in the user's pricelist, because each user is assigned particular pricing. 不正常的是,每个商品的定价都取决于用户价格表中的价格,因为每个用户都被分配了特定的定价。 Not all users have a price assigned for every item. 并非所有用户都为每个商品分配了价格。

Here is the question: The requirement from management is that the user should be able to see every product, even if that product is not in the user's pricelist. 这是一个问题:管理层的要求是,即使该产品不在用户的价目表中,用户也应该能够看到该产品。 If the user has no pricelist entry for a productitem, the page should display, "Contact us for pricing." 如果用户没有产品项目的价目表条目,则页面应显示“与我们联系以进行定价”。 I have not been able to figure out how to do this with one query. 我还无法弄清楚如何用一个查询做到这一点。 Every join that I attempted involving the pricelist table threw out products for which the user didn't have a price assigned. 我尝试涉及价目表的每个联接都抛出了用户未为其分配价格的产品。 I am given only the ProductID and UserID. 仅给我ProductID和UserID。 I have put my code below, which is a mix of mysql and PHP. 我将我的代码放在下面,它是mysql和PHP的混合体。 It works, but it is clunky, especially seeing as I have to redo the second query over and over. 它可以工作,但是笨拙,尤其是因为我不得不一遍又一遍地重做第二个查询。 Please tell me how I really ought to be doing it. 请告诉我我该怎么做。

$query_product = sprintf("SELECT * , (items.Stock - (SELECT Coalesce(Sum(DetailQuantity),0) 
FROM orderdetails 
INNER JOIN orders ON OrderID = DetailOrderID 
INNER JOIN productitems ON orderdetails.ProductItemID = productitems.ProductItemID
INNER JOIN products ON productitems.ProductID = products.ProductID
INNER JOIN items ON productitems.ItemID = items.ItemID
WHERE OrderDate > ProductUpdateDate))*ProductLive 
AS NumLeft
 FROM productitems
 INNER JOIN products ON products.ProductID = productitems.ProductID
JOIN items ON productitems.ItemID = items.ItemID
WHERE products.ProductID = %s",GetSQLValueString($ProductID, "int"));
$product = mysql_query($query_products, $x) or die(mysql_error());
$row_product = mysql_fetch_assoc($product);

//after narrowing down to one productitem, either through user selections or while looping through $row_product:

$query_price = sprintf("SELECT Price FROM pricelists 
WHERE UserID = %s AND ProductItemID = %s", GetSQLValueString($UserID, "int"), GetSQLValueString($row_product['ProductItemID'], 'int'));
$price = mysql_query($query_price, $x) or die(mysql_error());
$row_price = mysql_fetch_assoc($price);
$row_product['Price'] = $row_price['Price'];

Later in the code, I check whether $row_products['Price'] is empty or not to determine what is displayed on the page. 在代码的后面,我检查$ row_products ['Price']是否为空,以确定页面上显示的内容。

EDIT: I thought this would be obvious, but ... I have to limit pricelists by WHERE UserID = %s. 编辑:我认为这很明显,但是...我必须通过WHERE UserID = %s.限制价格表WHERE UserID = %s. That means I can't just tack on a LEFT JOIN pricelists ON pricelists.ProductItemID = productitems.ProductItemID , which was actually the first thing I tried, and which does not work. 这意味着我不能只在LEFT JOIN pricelists ON pricelists.ProductItemID = productitems.ProductItemID上添加LEFT JOIN pricelists ON pricelists.ProductItemID = productitems.ProductItemID ,这实际上是我尝试的第一件事,并且不起作用。 Limiting the outer join in the WHERE statement turns it into an inner join. 在WHERE语句中限制外部联接会将其转换为内部联接。

Add the pricelists table as an LEFT join to your main query: 将价目表作为左侧联接添加到您的主查询中:

SELECT field1, field2, ..., pl.*
FROM orderdetails 
INNER JOIN orders ON OrderID = DetailOrderID 
...
LEFT JOIN pricelists pl PN pl.ProductItemID = productitems.ProductItemID
..

In your code check if the price in the pricelists table is null, if so, show the text. 在您的代码中,检查价目表中的价格是否为空,如果为空,则显示文本。

You should look into using an outer join for this. 您应该考虑为此使用外部联接。 Heres a great description of the difference between an inner and outer join -> inner join vs outer join 这里很好地描述了内部联接和外部联接之间的区别-> 内部联接与外部联接

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

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