私は2つのテーブル:tbl_post
とtbl_tags
を持っています。 tbl_post
でカンマ区切りの値を含む列を選択すると、IDが含まれます
、フィールドtags
配列としてtbl_tags
からIDがを含有します。 例:( '15' 例)'1,3,15,20'
または'4,15,6,21'
私は、その場tags
がtbl_tags
からIDが含まれていtbl_post
からレコードを選択します。
どうすればいいですか?
私は2つのテーブル:tbl_post
とtbl_tags
を持っています。 tbl_post
でカンマ区切りの値を含む列を選択すると、IDが含まれます
、フィールドtags
配列としてtbl_tags
からIDがを含有します。 例:( '15' 例)'1,3,15,20'
または'4,15,6,21'
私は、その場tags
がtbl_tags
からIDが含まれていtbl_post
からレコードを選択します。
どうすればいいですか?
何について:
SELECT * FROM tbl_post WHERE tbl_post.tags LIKE '%15%';
ORあなたのコメントを1として
SELECT * FROM tbl_post WHERE Contains(tbl_post.tags, '15');
、あなたはあなたができるならば、これは
DECLARE @id INT = 15
DECLARE @stringId VARCHAR(50) = CAST(@id AS VARCHAR(50))
SELECT *
FROM tbl_post
WHERE tbl_post.tags = @stringId -- When there is only 1 id in the table
OR tbl_post.tags LIKE @stringId + ',%' -- When the id is the first one
OR tbl_post.tags LIKE '%,' + @stringId + ',%' -- When the id is in the middle
OR tbl_post.tags LIKE '%,' + @stringId -- When the id is at the end
の 'tags'が' 1,3,154'ならば何を使っていますか? – Blorgbeard
@Blorgbeard私はこれを処理するための答えを更新しましたが、効率的に設計されたDBスキーマよりもカンマで区切ったリストの値を使用するのは非常に良い理由が必要です。 – ske57
ああ、私は同意します。私はOPではない。ちょうどバグに気づいた。また、3つの別々の節の代わりに '%、15、% ''のような 'where('、 '+ tbl_post.tags +')のようなことをすることもできます。 – Blorgbeard
あなたのスキーマを変更する必要があります試みることができます。カンマで区切られた値を列に入れることは、クエリを扱いにくくするため、悪い考えです。 – Blorgbeard
@a_horse_with_no_name私は、 –