dongshike7171 2015-03-05 21:16
浏览 20
已采纳

临时表中varchar MySQL内的数值

Problem

I have a field with values like

 "ABC1234"
 "ABC5678/DEF"
 "AB1298"
 "AB1298/DEF"

I want to extract the numeric value from each one of it, such as:

  1234
  5678
  1298
  1298

NOTE: The numeric value is always "together" (1234) and is always composed by 4 digits only.

I was trying to delete first the double quotes to use RegEx: SELECT REPLACE(model_name,'''','') FROM ProductList Note: I replace using ' single quote instead of double quotes " , because that's the way the data was saved, and it works. And then I tried to use Patindex to get the numeric value: SELECT SUBSTRING(field, PATINDEX('%[0-9]%', field), LEN(field)) NOTE: However, PATINDEX does not work with MySQL

I'm trying to do this, because then I want to separate each value in two different columns by creating a temporary table:

SELECT SUBSTR(t.column_one, 1, INSTR(t.column_one, ' ')-1) AS col_one, SUBSTR(t.column_one, INSTR(t.column_one, ' ')+1) AS col_two FROM YOUR_TABLE t

   val1    val2
    12       34
    56       78
    12       98

Note: I'm using PHPMyAdmin within XAMPP.

After PATINDEX, I tried LOCATION, and POSITION. (You can see the sequence of the test print screen here ) SELECT SUBSTRING(model_name,LOCATE('%[^0-9]%',model_name),4) FROM ProductList NOTE: The LOCATE or POSITION function is returning a position 0, and that's why there is no result. I can imagine the problem is in "%[^0-9]%", because not LOCATE, POSITION nor MID accept RegEx.

My next problem is: I want to have the lval and rval inside the temporary table footprint, created at the beginning of the query. This, because I would like to create queries by getting input texts values, and have something like:

SELECT * FROM footprint WHERE lval=50;

      model_name     num_pos    lval     rval
      ''ABC1234''        7       12       34
    ''ABC1234/DEF''      7       50       78
    ''ABDCE1234''        8       12       98

Solution Proposal

At the end, I want to search the name_model, depending on the values of lval and rval. So the next "query" works for me:

CREATE TEMPORARY TABLE IF NOT EXISTS footprint AS
(SELECT model_name, 
  LEAST (
    if (Locate('0',model_name) >0,Locate('0',model_name),999),
    if (Locate('1',model_name) >0,Locate('1',model_name),999),
    if (Locate('2',model_name) >0,Locate('2',model_name),999),
    if (Locate('3',model_name) >0,Locate('3',model_name),999),
    if (Locate('4',model_name) >0,Locate('4',model_name),999),
    if (Locate('5',model_name) >0,Locate('5',model_name),999),
    if (Locate('6',model_name) >0,Locate('6',model_name),999),
    if (Locate('7',model_name) >0,Locate('7',model_name),999),
    if (Locate('8',model_name) >0,Locate('8',model_name),999),
    if (Locate('9',model_name) >0,Locate('9',model_name),999)
  ) AS num_Pos
FROM ProductList) ;
SELECT name FROM (SELECT name, left(val,2) AS lval, right(val,2) AS rval FROM
(SELECT MID(pl.model_name, fp.num_Pos,4) AS val, pl.model_name AS name FROM ProductList AS pl INNER JOIN footprint AS fp ON fp.model_name=pl.model_name) p) n WHERE lval='50' and rval='50'

If you have any other suggestion about how this process could be done or improved, please let me know.

Thank you, Best regards.

  • 写回答

3条回答 默认 最新

  • 普通网友 2015-03-06 15:48
    关注

    According to all the description I wrote, the next code ended working for me. A lot of hints are hiding in the description, but if you have any suggestion about the query, be my guest.

    CREATE TEMPORARY TABLE IF NOT EXISTS footprint AS
    (SELECT model_name, 
      LEAST (
        if (Locate('0',model_name) >0,Locate('0',model_name),999),
        if (Locate('1',model_name) >0,Locate('1',model_name),999),
        if (Locate('2',model_name) >0,Locate('2',model_name),999),
        if (Locate('3',model_name) >0,Locate('3',model_name),999),
        if (Locate('4',model_name) >0,Locate('4',model_name),999),
        if (Locate('5',model_name) >0,Locate('5',model_name),999),
        if (Locate('6',model_name) >0,Locate('6',model_name),999),
        if (Locate('7',model_name) >0,Locate('7',model_name),999),
        if (Locate('8',model_name) >0,Locate('8',model_name),999),
        if (Locate('9',model_name) >0,Locate('9',model_name),999)
      ) AS num_Pos
    FROM ProductList) ;
    SELECT name FROM (SELECT name, left(val,2) AS lval, right(val,2) AS rval FROM
    (SELECT MID(pl.model_name, fp.num_Pos,4) AS val, pl.model_name AS name FROM ProductList AS pl INNER JOIN footprint AS fp ON fp.model_name=pl.model_name) p) n WHERE lval='50' and rval='50'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号