使用嵌套的select

时间:2015-12-06 10:54:35

标签: mysql

我有以下名为 request 的mysql表:

requestID
requeststatusID
requestClientFirstName
requestClientLastName
requestClientNumber
requestClientEmail

我想获取requeststatusID不是4,5或6的所有记录,并获取以字母A开头的requestClientFirstName或以字母A开头的requestClientLastName或以字母A开头的requestClientEmail

还有一件事......如果用户输入2而不是字母A,我想得到所有以2开头的requestClientNumber ...

我正在尝试使用以下代码:

(SELECT *
FROM (`request`)
WHERE requeststatusID NOT IN (`4`, `5`, `6`))
LEFT JOIN
(SELECT * 
FROM (`request`) 
WHERE requestClientLastName LIKE (`A%`));

但代码悲惨失败......任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:2)

SELECT *
FROM `request`
WHERE requeststatusID NOT IN ('4', '5', '6')
AND CASE
    WHEN ('2' REGEXP '^[0-9]+$')  THEN
requestClientNumber LIKE '2%'
ELSE
( requestClientFirstName LIKE 'A%' OR  requestClientLastName LIKE 'A%' OR  requestClientEmail LIKE 'A%'  )
END