data parsing in query [Intermediate ~> Advance mysql user please help]

Discussion in 'MySQL' started by bartolay13, Feb 17, 2010.

  1. #1
    I was doing conditions in a single query
    here's what i've come up

    "SELECT DISTINCT store_type FROM stores
    WHERE NOT EXISTS (SELECT * FROM cities_stores
    WHERE cities_stores.store_type = stores.store_type)
    ";

    here's the question
    i want to parse a string from other table row where the string is formatted by php function "serialize($string)".. is there any way to parse the serialized string to mysql compatible type.?

    So much thanks...
     
    bartolay13, Feb 17, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, you can use REGEXP to test input string or [var]char field against regular expression match.
    Regards
    p.p.: also LIKE but REGEXP/RLIKE is better in this case.
    Edit: parsing can be done via string functions
    Note: mysql has not preg_replace!
    Maybe you'll provide some input and desired output
     
    koko5, Feb 17, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I would do something like this:

    SELECT store_type FROM stores
    WHERE store_type NOT IN (
    SELECT store_type FROM cities_stores)
    GROUP BY store_type;

    As far as the parsing question, I'm not clear on exactly what you are trying to accomplish. Are you saying there is a serialized string stored in the database?
     
    jestep, Feb 17, 2010 IP
  4. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #4
    yep.. alright here's the exact algorithm of what im trying to query..

    - i have a quiz with more than 300 items per quiz
    - its paginated so im only showing 5 questions per page
    - BUT it is random generated --> (order by rand() limit 0,5)
    - now once the user has finished that page it automatically updates the row and insert the answers WITH respects with the ITEM number.
    here is the string format of the answer
    q1:a;q9:d;q250:c; .....

    where question_item:user_answer;......

    now to the next page, again im querying random items BUT now here's what im trying to do with the query, all items that have already taken must restrict the query from selecting it randomly.. meaning items taken must not show again when random.

    i want to execute this without dealing into php to lessen memory load to the server, and besides some quizzes are about 1,000+ items and 50+ users are using it simultaneously.
     
    bartolay13, Feb 17, 2010 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    remember that i have to parse the string format of the answers and execute query from that string..
    stored procedures are also welcome, i just cant get the correct algorithm process of that.

    no php's please, i've already made that in php and convinced that i should replace it by pure mysql function and query.

    Thanks
     
    bartolay13, Feb 17, 2010 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    up :p:p:p:p
     
    bartolay13, Feb 18, 2010 IP
  7. anxggxun

    anxggxun Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    so a user just can have 1 row in the answers table right??
    i think the simple INSTR() function will help in this case, you can just search the question_item + ':' string in the answer string, if the function return is 0 means that the question was never been answered.

    SELECT * FROM question, answers
    where INSTR(answers.useranswer, question.question_item || ':' ) = 0 AND
    	answers.userid = 'theid'
    
    Code (markup):
    logicly i think it'll work but i don't know about the Load..


    note : use CONCAT() function if the || operator doesn't worked..
     
    anxggxun, Feb 22, 2010 IP