[英]How can i make inverse of IN Syntax?
IF I have a code like如果我有类似的代码
$vals = "50,60,40";
IN SQL IN SQL
"SELECT * FROM TABLE WHERE col IN( $vals )"
No if i have a value like 50 and i have col in SQL contain "50,40,60"不,如果我有一个像 50 这样的值并且我在 SQL 中有 col 包含“50,40,60”
How can i selected it我该如何选择它
I mean the inverse of IN Syntax我的意思是 IN 语法的逆
if i have table in sql contain values like如果我在 sql 中有表包含类似的值
Firstname | Lastname | Age | Hobbies
Ahmed | Ali | 50 | run,swim
Mohammed | Ahmed | 30 | run
and if i want who can swimming in Hobbies如果我想要谁可以在爱好中游泳
I mean the inverse of IN Syntax
我的意思是 IN 语法的逆
You could use the你可以使用
NOT IN
operator if you had a good table structure.运算符,如果你有一个好的表结构。
You should make a table for the Hobbies
and then add a relanshionship with your person
table, baecause as now the only way to get this done is with an ugly SQL containing lot of LIKE
condition.您应该为
Hobbies
制作一张表格,然后在您的person
表格中添加一个 relanshionship,因为现在完成此操作的唯一方法是使用包含大量LIKE
条件的丑陋 SQL 。
I recommend changing your schema to store the values in a separate table with a FK to the original column.我建议更改您的架构以将值存储在一个单独的表中,并带有原始列的 FK。
People
ID - PK
FirstName
LastName
Hobbies
ID -- PK
Person_ID -- FK to People
Hobby -- store your comma-separated values here
Then you can do然后你可以做
select People.ID, People.FirstName, People.LastName
from People
inner join Hobbies on People.ID = Hobbies.Person_ID
where Hobbies.Hobby = 'Swimming'
An even better structure might have 3 tables, allowing you to maintain a set of hobbies independently of the people and simply keeping a relation between each person and their hobbies.一个更好的结构可能有 3 个表,允许您独立于人们维护一组爱好,并简单地保持每个人与其爱好之间的关系。
People
ID
FirstName
LastName
Hobbies
ID
Hobby
PeopleHobbies
ID
Person_ID
Hobby_ID
As always, consider your query needs and create suitable indices on the tables to allow speedy lookups and joins.与往常一样,考虑您的查询需求并在表上创建合适的索引以允许快速查找和连接。
Check the FIND_IN_SET() function:检查FIND_IN_SET() function:
"SELECT * FROM TABLE WHERE FIND_IN_SET('swim', Hobbies) > 0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.