简单组合两个SQL表

时间:2016-02-21 13:41:33

标签: mysql

我需要帮助databaze输出一个。 我有这两个表:

OrganizationServiceProxy

在输出时我需要得到这个:

CallerId

不是这个:

我尝试了很多选项,但我总是写如下

systemuserid

我试过了 我的代码:

ANALYZE

    +----+------------+---------------------------------------+
    | ID | ID_analyze |                thread                 |
    +----+------------+---------------------------------------+
    |  4 |          1 | Failure of the power supply and media |
    |  5 |          2 | Failure of communication services     |
    +----+------------+---------------------------------------+

DATA
+----+------------+----------+
| id | ID_analyze |   norm   |
+----+------------+----------+
|  1 |          1 | A.11.2.1 |
|  2 |          1 | A.11.2.2 |
|  3 |          1 | A.12.3.1 |
|  4 |          2 | A.11.2.4 |
|  5 |          2 | A.13.1.1 |
+----+------------+----------+

比你大家帮忙。

2 个答案:

答案 0 :(得分:0)

这是一个使用带有完整演示的用户变量的解决方案。

SQL:

--Data preparation for demo
create table `analyze`(id int, id_analyze int, thread varchar(200));
insert into `analyze` values
(4,1,''),
(5,2,'');
create table data (id int,id_analyze int, norm varchar(200));
insert into data values
(1,1,'A.11.2.1'),
(2,1,'A.11.2.2'),
(3,1,'A.12.3.1'),
(4,2,'A.11.2.4'),
(5,2,'A.13.1.1');

SELECT * FROM `analyze`;
SELECT * FROM data;

-- SQL needed
SET @last_id = 0;
SELECT 
    IF(d.display, a.id, '') ID,
    d.norm
FROM 
    (SELECT 
        id_analyze, 
        norm, 
        (id_analyze-@last_id) <> 0 display,
        @last_id:=id_analyze
     FROM data) d,
    `analyze` a
WHERE
    d.id_analyze = a.id_analyze;

输出:

mysql> SELECT * FROM `analyze`;
+------+------------+--------+
| id   | id_analyze | thread |
+------+------------+--------+
|    4 |          1 |        |
|    5 |          2 |        |
+------+------------+--------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM data;
+------+------------+----------+
| id   | id_analyze | norm     |
+------+------------+----------+
|    1 |          1 | A.11.2.1 |
|    2 |          1 | A.11.2.2 |
|    3 |          1 | A.12.3.1 |
|    4 |          2 | A.11.2.4 |
|    5 |          2 | A.13.1.1 |
+------+------------+----------+
5 rows in set (0.00 sec)

mysql> SET @last_id = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    ->     IF(d.display, a.id, '') ID,
    ->     d.norm
    -> FROM
    ->     (SELECT
    ->         id_analyze,
    ->         norm,
    ->         (id_analyze-@last_id) <> 0 display,
    ->         @last_id:=id_analyze
    ->      FROM data) d,
    ->     `analyze` a
    -> WHERE
    ->     d.id_analyze = a.id_analyze;
+------+----------+
| ID   | norm     |
+------+----------+
| 4    | A.11.2.1 |
|      | A.11.2.2 |
|      | A.12.3.1 |
| 5    | A.11.2.4 |
|      | A.13.1.1 |
+------+----------+
5 rows in set (0.00 sec)

答案 1 :(得分:-1)

我会使用存储当前ID的标志

$lastID = "";
for($i = 0; i < count($rows); $i++):
    if($rows[i]['id']!=$lastID){
         $lastID = $rows[i]['id'];

         //the ID just changed, print it
         echo $rows[i]['id'];
    }

    echo $rows[i]['norma'];

endforeach;

未经测试但逻辑正确。