2

我在一列中有这些数据

Steffi | ND Baumecker | Cassy

我想进行查询以查找上述任何内容是否存在于另一列中

其他列的示例(艺术家是列名)

Artist
Steffi
Derrick Carter
Ben Klock
Craig Richards

我不认为 LIKE 会在这里工作,所以想知道在进行匹配时我可以使用什么查询从“艺术家”列返回艺术家姓名 - 所以在上面的示例中将返回“Steffi”。

我是否还需要删除 | 之前和之后的空格?在第一列?

谢谢!

4

1 回答 1

4

If I understand properly your problem: you want to filter rows using values from a column and searching for these values in another column?

SELECT a.first_name, a.last_name, a.nickname
FROM artist AS a
WHERE a.related_nickname IN (
    SELECT sa.nickname
    FROM artist AS sa
    WHERE sa.popularity > 30
)

MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/any-in-some-subqueries.html

It seems that you are trying to achieve a complicated task and I'd advise you to try a couple of things.

  • Subqueries are useful but makes your queries much slower so using two queries might speed things up. The first query would pick the values that will be used for filtering and the second query will search rows.

  • If you filter by string, consider using indexes on your table: http://dev.mysql.com/doc/refman/5.1/en/create-index.html

于 2013-03-11T04:41:51.180 回答