MYSQL - 要在子查询中使用的SELECT AS别名

时间:2013-02-12 16:23:37

标签: mysql select subquery

我希望能够使用我在子查询中进一步选择的项目之一。 正如您在本示例中所看到的,我将问题中的“变量”设置为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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 FIDDLE DEMO

试试这个:

    <?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。

答案 2 :(得分: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的。

答案 3 :(得分:-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