English 中文(简体)
MYSQL - 寻找未经排序的文本
原标题:MYSQL - search field for unordered text
  • 时间:2012-04-27 11:47:56
  •  标签:
  • mysql

我有问题要调查以下数据:

id. | name | type

 1.    bob | a 
 2.    sam | ab
 3.    jim | abc
 4.  sarah | ad
 5.   john | a
 6.    eve | bca
 7.  nikki | ca

我不想在以下投入的基础上撰写我的SQL问题:

a,
ac,
b*,
ac*

我想要归还以下名字:

bob
john
sam
jim
eve
nikki

<>Reakdown:

a  = bob, john
ac = nikki  
b* = sam, jim, eve
ac*= jim, eve, nikki

<>可解释:

a  = find results that are JUST a
ac = find results that contain ONLY an a AND c
b* = find results which contains a b
ac*  find results that contain at least an a and a c

可以看出,类型一栏可包含不同订单中的类型(并非总会增加)。

我确信,可以通过将类别栏分为<代码>类型_a、 类型_b来做到这一点。 等等,然后在每一条中有<条码>bool。 但是,这可以达到<条码>z,我不想在桌旁再 2626条col!

如果希望得到任何帮助的话,那是有可能在单一询问中这样做的。

关于标题,我不知道什么叫它。

最佳回答

http://dev.mysql.com/doc/refman/5.1/en/regexp.html”

SELECT name FROM your_table WHERE
     type RLIKE  ^a+$                             -- only  a 
  OR type RLIKE  ^((a[ac]*c)|(c[ac]*a))[ac]*$     -- only & both  a  AND  c 
  OR type RLIKE  b                                -- at least one  b 
  OR type RLIKE  (a.*c)|(c.*a)                    -- at least  a  and  c 
;
问题回答
-- ac*, ac, a, b*

SELECT  *
FROM    mytable
WHERE   (
        type RLIKE  a 
        AND type RLIKE  c 
        )
        OR
        (
        type RLIKE  a 
        AND type RLIKE  c 
        AND NOT type RLIKE  [^ac] 
        )
        OR
        (
        type RLIKE  a 
        AND NOT type RLIKE  [^a] 
        )
        OR
        (
        type RLIKE  b 
        );

This won t use any indexes though.

如果您的表格为<代码>MyISAM,你可以储存这样的类型:

id   name     type
7    nikki    a c

(note the spaces) and use FULLTEXT functionality:

CREATE FULLTEXT INDEX fx_mytable_type ON mytable (type);

-- ac*
SELECT  *
FROM    mytable
WHERE   MATCH(type) AGAINST  +"a" + "c"  IN BOOLEAN MODE);


-- ac
SELECT  *
FROM    mytable
WHERE   MATCH(type) AGAINST  +"a" + "c"  IN BOOLEAN MODE);
        AND NOT TYPE RLIKE  [^ ac]  -- note the space

Set @ft_min_word_len = 1 for this to work.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签