SQL Server:对于具有相同id的列的表,需要单行数据

时间:2018-05-17 16:22:29

标签: sql sql-server

我需要两个具有相同id 1的行的所有列,但称为不同,以便我可以区分哪一列属于哪一行。

我有以下数据

CREATE TABLE IF NOT EXISTS `docs` 
(
    `id` int(6) unsigned NOT NULL,
    `rev` int(3) unsigned NOT NULL,
    `content` varchar(200) NOT NULL,
    `test` varchar(200) NOT NULL,
    PRIMARY KEY (`id`,`rev`)
) DEFAULT CHARSET=utf8;

INSERT INTO `docs` (`id`, `rev`, `content`,`test`) 
VALUES ('1', '1', '11','33'),
       ('1', '2', '22','44');

我使用了以下查询:

SELECT 
    A.id, A.content as priorcontent, B.content as currcontent
FROM 
    docs A
INNER JOIN 
    docs B ON A.id = B.id

但我没有按预期得到结果

id  priorcontent    currcontent
-------------------------------
 1      11              11
 1      11              22
 1      22              11
 1      22              22

我想要的是

id  priorcontent  currcontent  priortest  currtest
---------------------------------------------------
1      11           22             33        44

提前致谢

4 个答案:

答案 0 :(得分:0)

似乎你希望两个分离的版本在同一行上具有相同的id 为了获得您展示的结果,您应该使用

version: '3'
networks:
  proxy:
    external: true
  internal:
    external: false
services:
  mysql:
    image: 'mysql:5.7'
    environment:
      MYSQL_ROOT_PASSWORD: null
    ports:
      - "3306:3306"
    networks:
      - internal
    labels:
      - traefik.enable=false
  portainer:
    image: portainer/portainer
    networks:
      - internal
      - proxy
    labels:
      - "traefik.frontend.rule=Host:portainer.xxx.xyz"
      - "traefik.port=9000"
      - "traefik.backend=portainer"
      - traefik.docker.network=proxy
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    restart: unless-stopped
  Nginx-Pro:
    image: nginx:alpine
    build:
      context: .
      dockerfile: Dockerfile_Nginx
    container_name: Nginx-Pro
    labels:
      - traefik.backend=Nginx-Pro
      - traefik.frontend.rule=Host:xxx.fr,www.xxx.fr, yyy.fr, www.yyy.fr
      - traefik.docker.network=proxy
      - traefik.frontend.passHostHeader=true
      - "traefik.port=80"
      - "traefik.frontend.entryPoints=http,https"
    networks:
      - internal
      - proxy
    depends_on:
      - mysql
    volumes:
      - /var/www/nginx-pro/:/usr/share/nginx/
      - /etc/nginx/nginx-pro/:/etc/nginx/
    links:
      - php-pro
  Nginx-Dev:
    image: nginx:alpine
    build:
      context: .
      dockerfile: Dockerfile_Nginx
    container_name: Nginx-Dev
    labels:
      - traefik.backend=Nginx-Dev
      - traefik.frontend.rule=Host:trombi.xxx.xyz
      - traefik.docker.network=proxy
      - traefik.frontend.passHostHeader=true
      - "traefik.port=80"
      - "traefik.frontend.entryPoints=http,https"
    networks:
      - internal
      - proxy
    depends_on:
      - mysql
    volumes:
      - /var/www/nginx-dev/:/usr/share/nginx/
      - /etc/nginx/nginx-dev/:/etc/nginx/
    links:
      - php-dev
  php-pro:
    container_name: php-pro
    image: php:7.2-fpm-alpine3.7
    build:
      context: .
      dockerfile: Dockerfile_PHP
    labels:
      - traefik.docker.network=proxy
      - "traefik.port=9000"
    networks:
      - internal
      - proxy
    volumes:
      - /var/www/nginx-pro/:/usr/share/nginx/
  php-dev:
    image: php:7.2-fpm-alpine3.7
    container_name: php-dev
    build:
      context: .
      dockerfile: Dockerfile_PHP
    labels:
      - traefik.docker.network=proxy
      - "traefik.port=9001"
    ports:
      - 9001:9000
    networks:
      - internal
      - proxy
    volumes:
      - /var/www/nginx-dev/:/usr/share/nginx/

答案 1 :(得分:0)

不确定您在这里寻找的是什么,但这将适用于此特定数据/请求:

 select a.id, a.content priorcontent, b.content currcontent, a.test priorcontent2, b.test currcontent2 
  from docs a join docs b on a.rev+1=b.rev and a.id=b.id

答案 2 :(得分:0)

SELECT A.id, 
A.content as priorcontent, B.content as currcontent,
A.test as priortest, B.test as currtest
FROM docs A
inner JOIN docs B ON A.id = B.id and A.rev < B.rev

答案 3 :(得分:0)

您正在ID列上进行匹配。似乎在一个或两个表中出现多次ID,例如如果ID 1出现在tableA中两次而TableB出现3次,则会看到6(3 x 2)行。由于您看到4行,因此它们都有2个副本,或者有4个具有相同ID的副本。