[英]In MySQL, how do I do an insert based on a join condition?
Thanks for looking! 谢谢你的期待!
I have virtually no experience with the LAMP stack, but I have recently started an online store that uses OpenCart which is based on the LAMP stack. 我几乎没有使用LAMP堆栈的经验,但我最近开始使用基于LAMP堆栈的OpenCart的在线商店 。
The store allows for me to assign reward points to customers and I am giving each new customer 10 points just for creating an account. 商店允许我为客户分配奖励积分,我为每个新客户提供10分仅用于创建账户。
Unfortunately, the OpenCart admin GUI only allows me to do this manually, one user at a time. 不幸的是,OpenCart管理GUI只允许我手动执行此操作,一次一个用户。
I do, however, have access to an interface that will allow me to run MySql commands and I would like to solve the problem using this approach. 但是,我可以访问一个允许我运行MySql命令的界面,我想用这种方法解决问题。
Within the database created by OpenCart, I have a oc_customer
table which contains all of my customers, and then I have a oc_customer_reward
table which keys off of the customer id and assigns a new record each time a customer is awarded points. 在OpenCart创建的数据库中,我有一个
oc_customer
表,其中包含我的所有客户,然后我有一个oc_customer_reward
表,它关闭客户ID并在每次客户获得积分时分配一条新记录。 Here is what that looks like: 这是看起来像:
How do I write a MySql query that will see if a customer from oc_customer
does NOT exist in the oc_customer_reward
table and IF THEY DO NOT EXIST, then create a record in the oc_customer_reward
table (worth 10 points) for that customer? 如何编写一个MySql查询,查看
oc_customer
中的客户是否不存在于oc_customer_reward
表中,如果他们不存在,那么在该客户的oc_customer_reward
表(价值10点)中创建一条记录?
I am not asking for working code (unless you really want to provide it) and I am willing to do the work myself, but frankly I don't know where to start. 我不是要求工作代码(除非你真的想提供它)并且我愿意自己做这项工作,但坦率地说我不知道从哪里开始。 What would be the approach?
方法是什么?
Per Olaf's suggestion, I am able to get all customers NOT in the oc_customer_reward
table with his suggested query: 根据Olaf的建议,我可以使用他建议的查询让所有客户
oc_customer_reward
表中:
SELECT customer_id
FROM oc_customer
WHERE customer_id NOT
IN (
SELECT customer_id
FROM oc_customer_reward
)
Now, I just need to iterate the results of that query (sorry I am not a database guy!) and insert a row in oc_customer_reward
for each of them. 现在,我只需要迭代该查询的结果(抱歉,我不是数据库人员!)并在
oc_customer_reward
为每个人插入一行。 Kind of like a foreach
loop in C#. 有点像C#中的
foreach
循环。 Any thoughts? 有什么想法吗?
First find customers not in the reward table: 首先找不到奖励表中的客户:
select customer_id from oc_customer
where customer_id not in (select customer_id from oc_customer_reward);
then take that and insert an entry into the reward table: 然后接受并在奖励表中插入一个条目:
insert into oc_customer_reward (customer_id, points)
select customer_id, 10 from oc_customer
where customer_id not in (select customer_id from oc_customer_reward);
This is not tested, but I hope it helps for a start. 这没有经过测试,但我希望它有助于一个开始。
Not tested and unsure about the table structure of oc_customer but following query should help you 没有测试和不确定oc_customer的表结构,但以下查询应该可以帮助您
INSERT INTO oc_customer_reward ( customer_id, description, points, date_added )
SELECT customer_id, 'reward for registration', 10, CURRENT_TIMESTAMP FROM oc_customer WHERE NOT EXISTS ( SELECT * FROM oc_customer_reward r WHERE r.customer_id = oc_customer.customer_id );
More details about the EXISTS statement you can find here 有关EXISTS语句的更多详细信息,请参见此处
http://dev.mysql.com/doc/refman/5.5/en//exists-and-not-exists-subqueries.html http://dev.mysql.com/doc/refman/5.5/en//exists-and-not-exists-subqueries.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.