简体   繁体   中英

Select by integer range in splitted string?

I'm new to MySQL and I currently have following problem:

I have a table "offers" where a user can place an offer to another user where he offers him ingame items. The items are placed into the datafield with the ItemID and ItemLevel separated by a ":" and the itemes by a ",".

Example: "0:346,2:638,1:646" = offers Item with ID 0 Level 346 and ID 2 Level 638 and ID 1 Level 646

Now I want to create a query with PHP to SELECT only offers with specific IDs and a range of Level.

Should I rebuild the whole thing and do it with 2 different tables "orders" and "order_data" or is it possible to make this filtering possible by a simple query?

You must normalize your data... As you say, you should distribute the orders data between multiple tables and columns.

However, there's a possible (and simple) solution for your request:

SELECT * FROM orders
WHERE 1=1
AND FIND_IN_SET('0:346', orderData)
;

FIND_IN_SET function returns the index of the searched string - if it exists on column - so it's usually used as a SELECT field in order to know string's index on comma separated string. But, as in this case, you can use it on WHERE clause in order to return only the records that contains the specific string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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