0

我希望能够使用我在子查询中进一步选择的项目之一。正如您在此示例中看到的,我已将有问题的“变量”设置为 100,但实际上这将是一个动态数字,因此我需要能够在子查询中访问它。

INSERT INTO users_friends (userId, friendId) 
SELECT 77, id as noob
FROM users WHERE email = 'a@g.com' 
AND
NOT EXISTS (SELECT * FROM users_friends
WHERE userId = 77 and friendId = noob)
LIMIT 1
4

4 回答 4

2

我不完全确定我理解你的问题,但 NOT EXISTS 就像 LEFT JOIN 和 IS NULL 一样工作。所以我认为这会起作用:

SELECT 77, @noob
FROM users u
  JOIN (SELECT @noob:= 100) r
  LEFT JOIN users_friends uf on u.userid = uf.userid and uf.friendid = @noob
WHERE email = 'a@g.com' 
  AND uf.userid IS NULL

这是SQL Fiddle

于 2013-02-12T16:35:53.383 回答
0

我认为最安全的方法是在子查询中定义这些。我通常给它一个 const 的别名:

INSERT INTO users_friends (userId, friendId) 
    SELECT const.userId, const.noob
    FROM users cross join
         (select 77 as userId, 100 as noob) const
    WHERE email = 'a@g.com' AND
          NOT EXISTS (SELECT *
                      FROM users_friends
                      WHERE userId = const.userId and friendId = const.noob
                     )
    LIMIT 1

我担心 SGeddes 的方法,因为它依赖于对查询范围之外的变量的正确评估。这在这种情况下可能有效,但我更喜欢查询不依赖外部变量的解决方案。顺便说一句,这也应该适用于任何数据库,并且不是特定于 MySQL 的。

于 2013-02-12T16:54:18.160 回答
0

编辑

我建议更改您的表结构。

    CREATE TABLE users_friends(
userid int, 
friendid int,
primary key (userid, friendid)
);

CREATE TABLE users (
 userid int primary key, 
 email varchar(100), 
 name VARCHAR (100),
 index (email,name)
);


INSERT INTO users VALUES (1, 'a@g.com', 'noob'), (2,'b@g.com', 'Joe');


INSERT INTO users_friends (userId, friendId) 
VALUES (2, (SELECT userId 
FROM users 
WHERE email = 'a@g.com' 
AND name = "noob"
AND NOT exists (SELECT * FROM users_friends as uf 
            JOIN users as u 
            ON u.userid = uf.userid 
            where uf.friendid = 2 AND name = "noob"            
           )
           ) 
       );

SQL 小提琴演示

尝试这个:

    <?php

    function select_query ($userid,$friendname, $email){
    $host = "host";
    $user = "username";
    $password = "password";
    $database = "database name";

    // open connection to databse
    $link = mysqli_connect($host, $user, $password, $database);
            IF (!$link){
                echo ("Unable to connect to database!");
            }
            ELSE {
               //Is someone registered at other conference from table registration
               $query = "    INSERT INTO users_friends (userId, friendId) 
VALUES (".$userId.", (SELECT userId 
FROM users 
WHERE email = '".$email."' 
AND name = '".$friendname."'
AND NOT exists (SELECT * FROM users_friends as uf 
            JOIN users as u 
            ON u.userid = uf.userid 
            where uf.friendid = ".$userId." AND name = '"$friendname"'            
           )
           ) 
       )"; 
               $result = mysqli_query($link, $query);
               return $query;
               return $result;
            }
            mysqli_close($link);
    }

    echo select_query(1,noob,'a@g.com');
   ?>

就像我上面提到的,我不确定你的意思。如果您的意思是动态的,您可以更改变量的值,这可能会有所帮助。在您之前的帖子中,您使用了 PHP。所以,我的猜测是你正在使用 PHP。

于 2013-02-12T16:52:32.690 回答
-1
INSERT INTO users_friends (userId, friendId) 
SELECT 77, id 
FROM users WHERE email = 'a@g.com' 
AND
NOT EXISTS (SELECT * FROM users_friends
WHERE userId = 77 and friendId = (SELECT id 
FROM users WHERE email = 'a@g.com'))
LIMIT 1
于 2013-02-12T19:13:59.590 回答