加入LIKE语句

时间:2014-01-17 20:20:58

标签: mysql arrays join

我有两张桌子:

+--------------+     +------------------+
| files        |     | locked           |
|--------------|     |------------------|
| filename     |     | user   | filename|
+--------------+     +------------------+
| /f1/f2/xy.txt|     |  77    | /f1     |
| /f3/xx/xx.doc|     |  65    | /abc    |
| /x.txt       |     |  77    | /f3/xx  |
+--------------+     +------------------+
(The're a lot more files and locked filenames, but so its easier to understand)

files包含我所有文件的列表。表locked包含一个包含文件夹的列表。 例如,在第一行中定义user 77不允许files看到以f1开头的SELECT filename FROM files f LEFT JOIN locked l ON f.filename LIKE CONCAT(l.filename,'%') WHERE user='77';

所以我的愿望是这样的声明:

NOT IN(SELECT...)

我无法使用{{1}}执行此操作,因为它会变慢。

1 个答案:

答案 0 :(得分:1)

http://sqlfiddle.com/#!2/12f23/5/0

SELECT f.filename FROM files f
LEFT JOIN locked l ON user=77 AND LOCATE(l.filename, f.filename) = 1
WHERE l.filename IS NULL;

结果:

FILENAME
/x.txt