MYSQL where子句加入字符串搜索

时间:2014-02-06 04:45:26

标签: mysql

我很难形容这一点,我会告诉你我想要

albums_lookup具有结构

lookup    |    id

其中lookup的格式为uid / title

我的查询

SELECT `id`
FROM
    `albums_lookup`
WHERE
    `lookup` = (SELECT 
            `id`
        FROM
            `users`
        WHERE
            `username` = 'someusername') + '/sometitle'

你可以告诉我,我有一个用户名,我需要先得到用户的id,然后在where子句中附加文本'/ sometitle'

2 个答案:

答案 0 :(得分:0)

您应该使用CONCATJOIN

SELECT id
FROM albums_lookup al
INNER JOIN users u
   ON CONCAT(al.lookup, '/sometitle') = u.id
WHERE u.username = 'someusername';

答案 1 :(得分:0)

SELECT `id`
FROM
`albums_lookup`
WHERE
`lookup` = concat_ws((SELECT 
        `id`
    FROM
        `users`
    WHERE
        `username` = 'someusername') ,'sometitle');