简体   繁体   English

如何从表中选择记录,其中记录具有与另一个表最相似的属性

[英]how to select record from table where record have the most similar attribute from another table

TABLE A 表A
result select 结果选择
A a a
B a a
B b b
C a a
C b
C c

TABLE B 表B

user select 用户选择

1 a 1个
1 b 1例
2 b 2例
2 c 2点

TABLE C 表C
user result 用户结果
1 B 1个
2 C 2℃


what mysql query should i have to use, if i want to do like this: 如果我想这样做,我应该使用什么mysql查询:

if user select a the result is ABC (because ABC have 'a' in column 'select') if user select ab the result is BC (because A doesn't have 'b' in column 'select') if user select abc the result is C (because C have 'abc' in column 'select') if user select bc the result is C (because AB doesn't have 'c' in column 'select') if user select c the result is C (because AB doesn't have 'c' in column 'select') 如果用户选择一个结果,则结果为ABC(因为ABC在“选择”列中有“ a”);如果用户选择a,则结果是BC(因为A在“选择”列中没有“ b”);如果用户选择了abc,则如果用户选择bc,则结果为C(因为C在“选择”列中具有“ abc”),如果用户选择b,则结果为C(因为AB在“选择”列中不具有“ c”),如果用户选择c,则结果为C(因为AB在“选择”列中没有“ c”)

so it will select the record that have the most similar from what user selected and ignore the other record that doesn't match.. sorry for my english, i'm speak bahasa... 因此它将从用户选择的内容中选择最相似的记录,而忽略其他不匹配的记录..对不起我的英语,我说巴哈萨语...

SELECT  `result`
FROM tableA
WHERE   `select` IN ('a','b','c')
GROUP BY result
HAVING COUNT(*) = 3

if a unique constraint was not enforce on select for every result , a DISTINCT is required. 如果没有对每个result select施加唯一约束,则需要DISTINCT

SELECT  `result`
FROM tableA
WHERE   `select` IN ('a','b','c')
GROUP BY result
HAVING COUNT(DISTINCT `select`) = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Yii从表中选择在相关表中没有记录的第一条记录 - Yii select first record from table where it does not have a record in a related table 试图从表B中选择所有与表A中最近添加的记录同名的记录 - Trying to select all the records from table B that have the same name as most recently added record from table A 如何从表1中选择还没有付款条目的记录? - How to select record from table1 where there is no payment entry yet? 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query 从另一个表获取记录 - Getting a record from another table 将记录从一个表复制到另一个表+添加自己的记录 - Copy record from a table to another table + add own record 如何使用来自另一个表的随机记录更新给定的mySQL表 - How to UPDATE a given mySQL table with random record from another table Codeigniter 显示表 2 中每条 id 与表 1 相似的记录的记录 - Codeigniter display records from table 2 for each record with id similar to table 1 如何使用活动记录从col1 + col2 = b的表中选择* - how to select * from table where col1+col2=b using active record 查询用另一个表中的另一个随机记录更新记录? - query to update record with another random record from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM