简体   繁体   English

从MYSQL获取数据时如何从逗号分隔的数组中获取单个值

[英]how to get single value from comma separated array when fetching data from MYSQL

I am entering data in data base like this: 我正在这样的数据库中输入数据:

ID | Name | Email   | Place
1  | Khan | abc@... | Page1, Page2, Page10

Now I am fetching data like this : 现在,我正在像这样获取数据:

$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "SELECT * FROM story Where Place='Page2' Limit 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

But unfortunately it show me nothing :( 但不幸的是,它什么都没给我显示:(

I am uploading this Place row in comma separated form, now I want it show me the data where I select place. 我正在以逗号分隔的形式上传此Place行,现在我希望它向我显示选择地方的数据。

Please consider normalizing your data. 请考虑规范化您的数据。 Is storing a delimited list in a database column really that bad? 将分隔列表存储在数据库列中真的不好吗? The design you have here will make you crazy if your application actually grows. 如果您的应用程序确实在增长,这里的设计会让您发疯。

In the meantime, you want to use column LIKE '%value%' in place of column = 'value' . 同时,您想使用column LIKE '%value%'代替column = 'value' Beware: the performance of this kind of query is very bad and can't be improved. 当心:这种查询的性能很差,无法改进。

EDIT Queries using column = 'value' or even column LIKE 'value%' can exploit indexes in the database management system. 编辑使用column = 'value'或什至column LIKE 'value%'可以利用数据库管理系统中的索引。 It's possible to look up values in an index in O(log(n)) time. 可以在O(log(n))时间内在索引中查找值。 That permits even a vast table, with billions of rows, to be accessed efficiently. 这甚至可以有效地访问具有数十亿行的巨大表。 However, a query containing column LIKE '%value%' has to scan through all the data, row by row, exhaustively looking for matches, in O(n) time. 但是,包含column LIKE '%value%'的查询必须在O(n)时间内逐行扫描所有数据,以详尽地寻找匹配项。 If you have a few hundred rows you won't notice this poor performance. 如果您有几百行,您将不会注意到这种糟糕的性能。 But if your table grows you will definitely be sorry. 但是,如果您的桌子增加了,您一定会后悔的。

Try this query. 试试这个查询。

     SELECT * 
       FROM table
      WHERE column LIKE CONCAT('%' , 'Page2', '%')
      LIMIT 1

I suggested the use of CONCAT() so the query can easily be modified to use bind parameters. 我建议使用CONCAT()以便可以轻松地修改查询以使用绑定参数。 For example, CONCAT('%' ,? , '%') 例如, CONCAT('%' ,? , '%')

Another edit. 另一个编辑。 If you have Page1, Page100, Page101 you'll get a lot of false positive matches on Page1 . 如果您具有Page1, Page100, Page101Page1上会出现很多误报匹配。 In MySQL FIND_IN_SET() can do a search of a comma-separated string. 在MySQL中, FIND_IN_SET()可以搜索逗号分隔的字符串。

     SELECT * 
       FROM table
      WHERE FIND_IN_SET('Page2', column) <> 0
      LIMIT 1

This will not work if you have spaces after your commas. 如果逗号后有空格,则此功能将无效。 Page1,Page2,Page3 is fine, but Page1, Page2, Page3 is no good. Page1,Page2,Page3很好,但是Page1, Page2, Page3不好。 We can patch that problem like so. 我们可以像这样修补该问题。

     SELECT * 
       FROM table
      WHERE FIND_IN_SET('Page2', REPLACE(column, ', ', ',')) <> 0
      LIMIT 1

But now things are getting out of hand. 但是现在事情变得一发不可收拾了。 What if you have two spaces after a comma someplace? 如果在逗号后有两个空格怎么办? Or three? 还是三个?

This too is an unoptimizable search. 这也是无法优化的搜索。

A move toward data normalization. 向数据标准化迈进。

A proposed junction table (eradicates the Place CSV column): 建议的联结表(消除“放置CSV”列):

create table story_place_jnc
(   -- one row per story/place combo
    id int auto_increment primary key,
    storyId int not null,
    place varchar(20) not null  -- you size this thing

    -- perhaps 2 FK's go here (1 to story, 1 to place):

    -- Unique composite key on (storyId,place):
);

As Patsy said in Monty Python's Holy Grail, "It's only a model." 正如Patsy在Monty Python的圣杯中所说,“这只是一个模型。”

Who knows if you even have a place table :> 谁知道,你甚至有一个place表:>

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

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