开发者

I need to return a set of names only if they match in another tables text field

I am t开发者_如何学Gorying to get a Select statement to work, I have a table(districts) with Names field and I only want to return a name if it is contained in a text field in another table. Something like this.

SELECT name  FROM districts,users 
WHERE users.accesslist LIKE '"%"+districts.name+"%"'

This returns an empty list.

I can get it to work if I use a specific name %Swindon% so is it my concatenation that is wrong?


Looks like it is. Use CONCAT("%", districts.name, "%")


You need to specify some sort of relationship between the tables. As it stands now, you're selecting from two tables, but specifying a condition on only one of them.

SELECT name
FROM districts, users
WHERE users.accesslist LIKE '%{$district_name}%' AND districts.name = users.district

would be an example. You didn't specify your table layout, but there must be a field that's common between the two tables, and those are the ones you specify the join condition on.

As well, your query has a syntax error. The doublequotes within the LIKE clause shouldn't be there.


Without knowing the table schema (primary/foreign keys) a subquery should do it. But it is horrible. If you can give use the table schema we can give you something bettter.

This will return all the names in district if they are in users.

SELECT name
FROM districts AS d
WHERE d.name = SELECT name FROM users AS u 


LIKE '%Example%' command return All fields like ...AAExampleBB... LIKE '%Example' command return All fields like ...AAExample LIKE 'Example' command return All fields like Example You can use REGEX '^Example$' too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜