简体   繁体   English

PHP / MySQL有SELECT…INSERT和/或SELECT…INSERT…DELETE

[英]PHP/MySQL is there SELECT…INSERT and/or SELECT…INSERT…DELETE

I'm new to MySQL, and I'm trying to do the following and hoping I can do it all with a single PHP query to MySQL call rather than having to call, store in php, call, store in php etc. 我是MySQL的新手,我正在尝试执行以下操作,希望我可以通过一个PHP查询即可完成所有操作,而不必调用,存储在php中,调用,存储在php等中。

Here's my database setup. 这是我的数据库设置。 1. table of users, including email address. 1.用户表,包括电子邮件地址。 2. table of 'invitees' by email address and user who invited the invitee 3. table of 'friends' 2.通过电子邮件地址和邀请受邀者的用户的“被邀请者”表3.“朋友”表

I want to join the table of users with the table of invitees to determine which invitees have registered as users and who invited them (keeping in mind they might have been invited by more than 1 person). 我想将用户表与被邀请者表一起确定哪些被邀请者已注册为用户以及邀请了谁(请记住,他们可能已被1人以上邀请)。 From there I'll insert the newly registered invitee along with the person who invited him into the friends table. 从那里,我将把新注册的被邀请者以及邀请他的人插入到好友表中。 Finally I will delete that invitee from the invited table so that he won't keep receiving invitations. 最后,我将从被邀请者表中删除该被邀请者,以免他继续收到邀请。

I see how to do this with 3 separate php calls to mysql: 1. a JOIN to find the users who are in the user table and the invitee table (the join will also identify who invited them) 2. an INSERT to put the pair into the friends table 3. a DELETE to remove the user from the invitee list. 我看到了如何通过3个独立的php调用mysql来做到这一点:1. JOIN查找用户表和被邀请者表中的用户(联接还将标识邀请他们的人)2. INSERT将对进入好友表3.单击DELETE从受邀者列表中删除用户。

But this will be higher traffic/less optimized with multiple calls to the MySQL server and also a lot of data stored by my php script that I really just need for the next MySQL call. 但是,通过多次调用MySQL服务器以及我的php脚本存储的大量数据(这确实是我下一个MySQL调用所需要的),将会带来更高的流量/更少的优化。 I have a feeling this should be possible in just 1 call with no data returned to the php script. 我有一种感觉,只需1次调用就可以实现,而没有数据返回到php脚本。

I see there is an INSERT...SELECT MySQL call ( http://dev.mysql.com/doc/refman/5.0/en/insert-select.html ), but I have not been able to find a way to combine steps 1 and 2 above, which would be a SELECT...INSERT option. 我看到有一个INSERT ... SELECT MySQL调用( http://dev.mysql.com/doc/refman/5.0/en/insert-select.html ),但是我还没有找到一种结合的方法上面的第1步和第2步,这将是SELECT ... INSERT选项。 Does this exist? 是否存在? I tried the following, but it didn't work...something like this would be my ideal. 我尝试了以下方法,但是没有用...像这样的东西是我的理想选择。

SELECT users.user as user1, invited2.user as user2, invited2.email as email2 FROM users RIGHT JOIN invited2 ON users.email=invited2.invitee; 
INSERT INTO friends (user, friend) VALUES (user1, user2), (user2, user1);  
DELETE FROM invited2 WHERE invited2.email = email2; 

The part before INSERT INTO works on its own, but like I said, if I just return that to my php script, I'll just have to store all the results and then call the second myself. INSERT INTO之前的部分可以单独工作,但是就像我说的那样,如果仅将其返回到我的php脚本,则只需要存储所有结果,然后自己调用第二个即可。 Is there a way to do this in one call? 有没有办法在一个电话中做到这一点? And if so, is there a way to also tack on a delete statement (DELETE from invited2 WHERE invitee=users.email) afterwards? 如果是这样,此后是否有办法添加删除语句(从邀请的2删除(在被邀请的WHERE受邀者= users.email中))?

Any advice would be appreciated. 任何意见,将不胜感激。 Thanks in advance. 提前致谢。

MySQL does provide a INSERT...SELECT ( http://dev.mysql.com/doc/refman/5.1/en/insert-select.html ) MySQL确实提供了INSERT ... SELECT( http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

Your SQL can be edited to: 您的SQL可以编辑为:

INSERT INTO friends (user, friend)
SELECT users.user as user1, invited2.user as user2
FROM users
RIGHT JOIN invited2 ON users.email=invited2.invitee;

DELETE invited2
FROM users
RIGHT JOIN invited2 ON users.email=invited2.invitee;

In this case, INNER JOIN seems to make more sense than RIGHT JOIN. 在这种情况下,INNER JOIN似乎比RIGHT JOIN更有意义。

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

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