循环表记录

时间:2015-07-07 10:34:11

标签: mysql sql

我有一个表格,里面有一个名单,如下所示

+----------+
| CLI_NAME |
+----------+
| A        |
| B        |
| C        |
| D        |
| E        |
+----------+

我想循环使用像

这样的东西
for each value in CLI_NAME do
{
   bla bla
}

对于表CLI_NAMES中的每个值,我想循环并执行下面的内容。

从上表中选择的值需要作为T_NAME的参数输入,例如下面的查询中的T_NAME =“abc”

INSERT INTO TABLE_2(DATE, O_WT)
SELECT 
    F_DATE,
    CASE 
        WHEN
            SUM(
                CASE
                    WHEN
                        F_NAME like 'ty%asd%'
                    THEN
                        F_PKEY
                END
            ) = 1 THEN 1
        WHEN
            SUM(
                CASE
                    WHEN
                        F_NAME like 'ty%asd%'
                    THEN
                        F_PKEY
                END

            ) = 2 THEN 2               
        WHEN
            SUM(
                CASE
                    WHEN
                        F_NAME like 'ty%asd%'
                    THEN
                        F_PKEY
                END
            ) BETWEEN 3 AND 5 THEN  3 
        WHEN
            SUM(
                CASE
                       WHEN
                        F_NAME like 'ty%asd%'
                    THEN
                        F_PKEY
                END

            ) > 5 THEN 5                          
            ELSE 0 
    END AS O_WT
FROM
    TABLE_1 
WHERE 
    F_NAME IN (
        SELECT I_NAME from I_WD WHERE I_I_ID IN (
            SELECT I_MAP_ID FROM T_T_MAP where T_MAP_ID = (
                SELECT T_ID FROM TWD WHERE T_NAME = 'abc'
            )
        )
    ) 
    AND F_DATE between '2015-05-01' and '2015-06-01' 
    AND F_NAME LIKE 'ty%pr%' 
        GROUP BY F_DATE

我怎样才能循环播放&请这样做?

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您可以使用<div data-update="money.php" data-refresh-interval="500">

执行您想要的操作
CROSS JOIN

答案 1 :(得分:1)

所以你想要&#39; abc&#39; WHERE T_NAME = 'abc'替换为CLI_NAME?

这是否足够:

select i_map_id from t_t_map where t_map_id in 
(
  select t_id from twd where t_name in (select cli_name from cli_names)
)

而不是

select i_map_id from t_t_map where t_map_id = 
(
  select t_id from twd where t_name = 'abc'
)

它与循环不太相同。例如。如果&#39; A&#39;和&#39; B&#39;导致相同的t_ids,然后你只需要插入一次而不是两次。