简体   繁体   English

MySQL INSERT如果记录存在于另一个表中

[英]MySQL INSERT if record exists in another table

having a little trouble with a query that I'd like to keep to just one query. 在我只想查询一个查询时遇到一点麻烦。 Basically, I have two tables, users, and relationships. 基本上,我有两个表,用户和关系。 Users have an ID, the relationships table has user_a and user_b fields which contain users.id values, which matches users up as 'user_a follows user_b'. 用户具有ID,关系表具有user_a和user_b字段,其中包含users.id值,用户将用户匹配为“user_a follow user_b”。

Now I'm hitting the Twitter API to pull in user_a's followers (https://dev.twitter.com/docs/api/1/get/friends/ids) but people in the users table are people attending an event, and not necessarily in user_a's friends. 现在我正在使用Twitter API来吸引user_a的关注者(https://dev.twitter.com/docs/api/1/get/friends/ids),但是用户表中的人是参加活动的人,而不是必须在user_a的朋友中。 I need to execute an INSERT in the relationships table if user_b exists in the users table. 如果user_b中存在user_b,我需要在关系表中执行INSERT。

Here's what I have so far! 这是我到目前为止所拥有的!

$json = file_get_contents('https://api.twitter.com/1/friends/ids.json?cursor=-1&user_id=' . $this->_attendee->id);
$response = json_decode($json);
if($response->ids){
    foreach($response->ids as $friend){
        // query goes here
    }
} 

Build a select statement that returns the line you want to add exactly if the user entry exists. 构建一个select语句,如果用户条目存在,则返回要添加的行。 Then do INSERT INTO user_a SELECT YOUR_STATEMENT; 然后执行INSERT INTO user_a SELECT YOUR_STATEMENT;

eg. 例如。 INSERT INTO user_a SELECT uid, "value1", 2, "some other string" FROM user_b WHERE uid=$friend; INSERT INTO user_a SELECT uid,“value1”,2,“some other string”FROM user_b WHERE uid = $ friend;

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

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