身長と体重のために同じテーブルの列から2つのクエリの結果を取得する方法

DJタイガ:

私は基本的に両方含まれているテーブル持って身長体重、学生(日SIDを)。身長体重は同じ列に含まれています'Percent'と列挙列は身長と体重を含有します。両方のエントリはまったく同じ日付を持っています:

-- Table structure and sample data for table `PROGWandH`
--
CREATE TABLE `wandh` (
  `Wid` int(24) NOT NULL,
  `sid` int(4) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `level` enum('height','weight') NOT NULL,
  `Percent` decimal(5,1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `PROGWandH` (`Wid`, `sid`, `date`, `level`, `Percent`) VALUES
(157, 55, '2020-01-13 15:00:00', 'weight', '20.2'),
(131, 55, '2019-12-12 15:00:00', 'weight', '19.8'),
(121, 55, '2019-11-14 15:00:00', 'weight', '19.3'),
(774, 55, '2020-01-13 15:00:00', 'height', '113.0'),
(749, 55, '2019-12-12 15:00:00', 'height', '112.0'),
(739, 55, '2019-11-14 15:00:00', 'height', '111.5');
--
ALTER TABLE `wandh`
  ADD PRIMARY KEY (`Wid`);

私は、列の高さ、幅、および日付のリストを生成しようとしています。PHP Iの使用は1セットごと、次々に生成しますが、出力ではないので、私は、私がこれまでの重複を生成しようとした何等の身長、体重、日付、身長、体重、などのリストを生成する必要があります同期:

select Percent, s1.Percent1, date
from wandh
JOIN (SELECT DISTINCT Percent as 'Percent1' from wandh where level = 'weight' and sid = 55) as s1
      where sid = 55 and level = 'height';

そして

select Percent, s1.Percent1, date
from wandh JOIN (SELECT Percent as 'Percent1' from wandh where level = 'weight' and sid = 55) as s1
      on date where sid = 55 and level = 'height';

私が試した他のバリエーションは動作しません。

select level, Percent, date from wandh where level like '%eight' and sid = 55;

私は何をしないのですか?

離れて渡します:

あなたは、条件付き集計でそれを行うことができます。

select sid, date,
  max(case when level = 'weight' then percent end) weight,
  max(case when level = 'height' then percent end) height
from wandh
group by sid, date

参照のデモを
結果:

| sid | date                | weight | height |
| --- | ------------------- | ------ | ------ |
| 55  | 2019-11-14 15:00:00 | 19.3   | 111.5  |
| 55  | 2019-12-12 15:00:00 | 19.8   | 112    |
| 55  | 2020-01-13 15:00:00 | 20.2   | 113    |

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=298540&siteId=1