开发者

How do I get distinct values from an array stored in MySQL?

开发者 https://www.devze.com 2022-12-22 02:33 出处:网络
I have a MySQL field which stores an array with 3 values: array(item1=>开发者_StackOverflow中文版;1123, item2=>5454, item3=>23432)

I have a MySQL field which stores an array with 3 values:

array(item1=>开发者_StackOverflow中文版;1123, item2=>5454, item3=>23432)

How can I query the database with PHP so that I get only distinct values of item2 and then arrange those results by the values of item3?


A lot more information is needed - like, how your database is structured.

Look into the DISTINCT() function and the ORDER BY clause of SQL


It much easier to store your array into text and something you can separate later. Using php you can do this. I'll try to work with your data here into something you can use. says you have the field items. If instead you had an array such as.

$items = array(1123,5454,23432);

You can then implode it with a symbol such as:

$items = implode('|',$items);

and then store this in the database under a fields such as items that would look like this:

1123|5454|23432

Then when you want to grab the data you just need to explode it with the same symbol:

$items = explode('|',$row['items']);

and you are back with your dataset:

$items[0] = 1123 $items[1] = 5454 $items[2] = 23432

and then can access the second item by grabbing element one of the array:

$items[1]

Hopefully that gives you a better understanding of it.

0

精彩评论

暂无评论...
验证码 换一张
取 消