检查子查询返回的内容

时间:2015-05-24 05:13:08

标签: mysql

我有这样的查询:

INSERT INTO cpus (
    `friendlyname`, `group
) VALUES (
    'something', (SELECT `key` FROM `groups` WHERE `key` = 'something')
);

但是,我想得到子查询的结果。最初我认为它会像

SELECT `key` FROM `groups` WHERE `key` = 'something' AS `result` 

但那并没有奏效。我还应该尝试什么?

1 个答案:

答案 0 :(得分:2)

您可以使用MYSQL IF来检查它是否返回任何内容

试试这个:

INSERT INTO cpus
(
`friendlyname`, 
 `group
) 
VALUES 
(
  'something', 
  IF((SELECT `key` FROM `groups` WHERE `key` = 'something') is null,'Do  something on success','Else' )
);

请按照以下链接获取有关MYSQL控制流程的更多详细信息: https://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html