简体   繁体   English

如何根据第三记录是否存在查询两个表?

[英]How to query two tables based on whether or not record exists in a third?

I have three tables, the first two fairly standard: 我有三个表,前两个表相当标准:

1) PRODUCTS table: 1) PRODUCTS表:

  • pid pid
  • pname, etc pname等

2) CART table: 2) CART表:

  • cart_id cart_id
  • cart_pid cart_pid
  • cart_orderid etc cart_orderid等

The third is designed to let people save products they buy and keep notes on them. 第三个目的是让人们保存他们购买的产品并在上面留下记号。

3) MYPRODUCTS table: 3) MYPRODUCTS表:

  • myprod_id myprod_id
  • myprod_pid myprod_pid

PRODUCTS. 产品。 prod_id = CART. prod_id = CART。 cart_prodid = MYPRODUCTS. cart_prodid = MYPRODUCTS。 myprod_pid

When a user orders, they are presented with a list of products on their order, and can optionally add that product to myproducts. 当用户下订单时,系统会向他们显示其订单上的产品列表,并且可以有选择地将该产品添加到myproducts中。 I am getting the info necessary for them to do this, with a query something like this for each order: 我正在获取他们执行此操作所需的信息,并针对每个订单进行如下查询:

SELECT cart.pid, products.pname, products.pid
FROM products, cart
WHERE products.pid = cart_prodid 
AND cart_orderid=orderid

This is fine the first time they order. 第一次订购时很好。

However, if they subsequently reorder a product which they have already added to myproducts, then it should NOT be possible for them to add it to myproducts again - basically instead of 'Add to MyProducts' they need to see 'View in MyProducts'. 但是,如果他们随后重新订购了已经添加到我的产品中的产品,则他们应该不可能再次将其添加到我的产品中-基本上,而不是“添加到我的产品”中,他们需要看到“在我的产品中查看”。

I am thinking I can separate the products using two queries: 我想我可以使用两个查询来分离产品:

  1. Products never added to MyProducts 从未添加到“我的产品”中的产品

    By somehow identifying whether the user has the product in MyProducts already, and if so excluding it from the query above. 通过某种方式确定用户是否已经在MyProducts中拥有该产品,如果可以,则从上面的查询中将其排除。

  2. Products already in MyProducts 我的产品中已有产品

    By reversing the process above. 通过逆转上述过程。

I need some pointers on how to do this though. 我需要一些有关如何执行此操作的指示。

Products they've ordered before: 他们之前订购的产品:

SELECT cart.pid, products.pname, products.pid 
from products p
inner join cart c on p.prodid = c.cart_prodid
WHERE cart_orderid=orderid

Products they've never ordered before 他们从未订购过的产品

SELECT products.pname, products.pid 
from products p
left join myproducts mp ON p.prodid = mp.prodid
WHERE mp.prodid IS NULL

The above query may read more naturally as a NOT IN statement, which is to follow. 上面的查询可能更自然地显示为NOT IN语句,随后将执行该语句。 I've prioritized the above because it is most likely a faster operation. 我已经优先考虑了上述内容,因为它很可能是更快的操作。

SELECT products.pname, products.pid 
from products p
WHERE p.prodid NOT IN 
(
  SELECT prodid
  FROM myproducts
)

Don't you need a User ID in your myproducts table? 您在myproducts表中是否不需要用户ID?

This is not going to answer your specific problem, but have you considered a change in the user interface? 这不会解决您的特定问题,但是您是否考虑过更改用户界面? Perhaps the user shouldn't control their list of purchased products. 也许用户不应该控制他们购买的产品列表。 This seems like you are unnecessarily complicating your interface. 看来您不必要地使界面复杂化。 You could automatically add every product they ordered to "My Products". 您可以将他们订购的每个产品自动添加到“我的产品”中。

Also, I think you should keep track of how many products the user ordered of the same product ID. 另外,我认为您应该跟踪用户订购了同一产品ID的产品数量。 Put a "count" field in the myproducts table to keep track of this. myproducts表中放置一个“ count”字段以进行跟踪。 You don't have to actually use this field, but you may eventually regret not adding this. 您不必实际使用此字段,但是最终您可能会后悔没有添加此字段。 Or even better - add a new row for every added product so you can keep track of the date/time as well. 甚至更好-为添加的每个产品添加新行,以便您也可以跟踪日期/时间。

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

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