简体   繁体   English

mysql的简单数据库查询

[英]simple database query for mysql

i am having a database in that 'users' table contain 4 fields id , name , gender , language 我在“用户”表中有一个数据库,其中包含4个字段id,name,性别,language
ex. 恩。 (database entries) (数据库条目)

1   jhon     male     en-sp-gr  
2   mira    female    sp-  
3   mike     male     en-  
4   shel    female    sp-gr  

etc... 等等...

here en,sp,gr are languages English , Spanish , German what i am trying to fetch the count of users who speaks only en,sp (english and spanish languages) the database contains more than 300 users and i am more more confused how to get the count of users who speaks only en and sp the language field of the table contains string zh_cn,sp,gr是英语,西班牙语,德语的语言。我试图获取仅讲en,sp(英语和西班牙语)语言的用户数量,数据库包含300多个用户,而我更困惑于如何获取只会说ensp的用户数,并且sp的表的language字段包含字符串

 en-sp-gr 

how can i omit -gr and hyphen ( - ) form the string and this only for one row i have to perform this for each row and then count of those users only who speaks en and sp. 我如何才能省略-gr和连字符( - )组成字符串,并且仅在一行中执行此操作,因此我必须为每一行执行此操作,然后仅对讲en和sp的那些用户计数。 i am not expert in sql :( 我不是sql方面的专家:(

You could do something like this: 您可以执行以下操作:

SELECT * 
FROM users 
WHERE language IN ('en-sp', 'sp-en')

However, you really should look into normalizing your database: 但是,您确实应该研究规范化数据库:

Users
ID|Name|Gender

Languages
ID|Code

User_Language_Map
UserID|LanguageID

Then you could do something like this: 然后,您可以执行以下操作:

SELECT *
FROM Users
WHERE ID IN
(
    SELECT UserID 
    FROM User_Language_Map
    WHERE LanguageID IN (IDFORSP, IDFORGR)
    GROUP BY UserID
    HAVING COUNT(DISTINCT LanguageID) = 2
)

If you cannot do that, and the language filter is going to be dynamic, then you really would need to create a function that will create the appropriate string combinations (or you could build a regex on the fly) and pass that into your IN . 如果您不能这样做,并且语言过滤器将是动态的,那么您确实需要创建一个函数来创建适当的字符串组合(或者您可以动态构建正则表达式)并将其传递到IN Otherwise, I don't know that you have many options as you are parsing strings at that point. 否则,我不知道在解析字符串时您有很多选择。

This is why you should not store multiple values in one field. 这就是为什么您不应该在一个字段中存储多个值的原因。 They should be stored in a normalized table instead. 它们应该存储在规范化表中。

But you can accomplish this by replacing the - with , and using FIND_IN_SET() . 但是,您可以通过将-替换为,并使用FIND_IN_SET()来完成此操作。 This is necessary because FIND_IN_SET() expects a comma-separated list of values. 这是必需的,因为FIND_IN_SET()期望用逗号分隔的值列表。 This can only be reasonably managed with a handful of languages though, since you need to code all the permutations into the query. 但是,这只能用几种语言进行合理管理,因为您需要将所有排列编码到查询中。 For this reason (among others like indexing) it is very strongly recommended to refactor this into a related table linking users to their spoken languages. 因此,强烈建议将索引重构为一个相关的表,以将用户链接到他们的口头语言。

SELECT COUNT(*) FROM users 
WHERE 
  FIND_IN_SET('en', REPLACE(language,'-',',')) > 0
  AND FIND_IN_SET('sp', REPLACE(language,'-',',')) > 0
  AND FIND_IN_SET('gr', REPLACE(language,'-',',')) = 0
SELECT 
      count(case when language = 'en' then language end) as English, 
      count(case when language = 'sp' then language end) as Spanish 
FROM users

In above query can count the English Language Users and Spanish Laguage User. 在上面的查询中可以计算英语语言用户和西班牙语语言用户。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM