Home  >  Q&A  >  body text

Find year range for Mysql REGEXP

I want to separate the content in a table on my movie website based on year intervals, but no matter how hard I try, I can't succeed. Actually, what I want is to list movie files before 1950. I don't know if I made a grammar mistake because I'm a beginner

A Movie 1910
B Movie 1920
C Movie 1930
D Movie 1940
E Movie 1950
F Movie 1970
G Movie 1990


SELECT * FROM streams WHERE type='movie' AND filename REGEXP '/^(190[1-9]\d|194\d)$/';

P粉545910687P粉545910687183 days ago417

reply all(1)I'll reply

  • P粉432930081

    P粉4329300812024-04-02 12:26:05

    The

    year is at the end of the filename, not the beginning, so you need to remove the ^ anchor.

    There is an extra

    0 after 19, so you are matching a 5-digit year starting with 190, not a 4-digit year starting with # Year starting with ##19.

    Do not place

    / delimiters around regular expressions in SQL.

    SELECT * FROM streams 
    WHERE type='movie' 
    AND filename REGEXP '19[0-4]\d$';

    If you are using MySQL 5.x instead of 8.x, it does not support escape sequences like

    \d. Replace it with [0-9] to match numbers.

    reply
    0
  • Cancelreply