在MySQL查询中苦苦挣扎

时间:2016-11-03 03:06:19

标签: php mysql

我有MySQL查询:

SELECT
  CONCAT(SUBSTRING(no_intern, 1, 2 * 1),
  MAX(SUBSTRING(no_intern, (2 * 2) -1, 2))
  ) AS menu
FROM tb_m_menu
WHERE no_intern LIKE (SELECT CONCAT(SUBSTRING(no_intern, 1, 2 * level_menu), '%') FROM tb_m_menu WHERE id_menu = 1)

结果是:0101

现在我希望结果总是增加一个:0101,0102,0103,但是当我像这样更改脚本时:

SELECT
  CONCAT(SUBSTRING(no_intern, 1, 2 * 1),
  MAX(SUBSTRING(no_intern, (2 * 2) -1, 2))+1 /*ditambahkan 1*/
  ) AS menu
FROM tb_m_menu
WHERE no_intern LIKE (SELECT CONCAT(SUBSTRING(no_intern, 1, 2 * level_menu), '%') FROM tb_m_menu WHERE id_menu = 1)

结果:012,013

如何制作:0102,0103,0104?

2 个答案:

答案 0 :(得分:0)

我认为你错误 +1 试试这个吧。我在 AS MENU

附近更改 +1 的位置
SELECT CONCAT(SUBSTRING(no_intern, 1, 2 * 1),
MAX(SUBSTRING(no_intern, (2 * 2) -1, 2)))+1 AS menu
FROM tb_m_menu
WHERE no_intern LIKE (SELECT CONCAT(SUBSTRING(no_intern, 1, 2 * level_menu), '%') FROM tb_m_menu WHERE id_menu = 1)

答案 1 :(得分:0)

(代表OP发布解决方案)

我使用此代码:

SELECT 
  CONCAT(SUBSTRING(no_intern, 1, 2 * 1), 
  CONCAT(REPEAT('0', 2-LENGTH(MAX(SUBSTRING(no_intern, (2 * 2) -1, 2))+1)), MAX(SUBSTRING(no_intern, (2 * 2) -1, 2))+1)
  ) AS menu
FROM tb_m_menu 
WHERE no_intern LIKE (SELECT CONCAT(SUBSTRING(no_intern, 1, 2 * level_menu), '%') FROM tb_m_menu WHERE id_menu = 3)