如何删除字符串的格式部分?

时间:2016-07-11 01:59:33

标签: mysql sql regex

我有这张桌子:

// image_path
+----+-----------------------------+
| id |           path              |
+----+-----------------------------+
| 1  | 3498uhrf34i098jf349jf43.png |
| 2  | mfij948t5498hj43h9f34p0.jpg |
| 3  | fcnh9348jmof9348i34f435.png |
| 4  | 3049jr430okf43p0ijfr43f.gif |
| 5  | oi3094jfo439ijf430f443r.jpg |
| 6  | 3f0irj03409iko4p3rfo04f.png |
+----+-----------------------------+

这是我的疑问:

SELECT * FROM image_path WHERE path = :token;
//                             ^^^^ removing everything from the right side until dot

所以这是一个例子:

SELECT * FROM image_path WHERE path = 'fcnh9348jmof9348i34f435';
/* output:
+----+-----------------------------+
| 3  | fcnh9348jmof9348i34f435.png |
+----+-----------------------------+
*/

我该怎么做?

3 个答案:

答案 0 :(得分:1)

如何使用like

where path like concat('fcnh9348jmof9348i34f435', '.%')

https://plnkr.co/edit/YugyLd8H5mQExzF61rA9?p=preview是一个SQL小提琴。

编辑:

如果您想在第一段时间之前获得该部分,请使用substring_index()

select substring_index(path, '.', 1)

答案 1 :(得分:1)

像这样(substring_index):

SELECT * FROM image_path WHERE substring_index(path, '.', '1') = 'fcnh9348jmof9348i34f435';

Demo Here

答案 2 :(得分:1)

您可以使用LEFTLOCATE函数:

    SELECT DISTINCT DateTime(Rounded, 'unixepoch') AS RoundedDate, -- Rounded DateTime to the floor hour
                    Count() AS Count, -- Count of items that registered within the above time
                   CAST (avg(Speed) AS INT) AS AverageSpeed, -- Average table.Speed column data within the defined datetime
                   SUM(CASE WHEN Speed > SpeedLimit THEN 1 ELSE 0 END) AS SpeederCount
    FROM RawSpeedLane AS sl