简体   繁体   English

如何根据某些列选择mysql记录并在表中多次显示?

[英]How do i select mysql record and display in table multiple times based on certain columns?

i have a table like this: 我有这样一张桌子:

id, name, date1, qty1, note1, date2, qty2, note2, date3, qty3, note3, admin

some example records: 一些示例记录:

1, something , 03-17-2017, 20, wow ,           ,   ,    ,           ,   ,     , greg
2, nothing   , 03-17-2017, 25, hmm , 03-18-2017, 26, ok ,           ,   ,     , dave
3, everything, 03-17-2017, 30, cool, 03-18-2017, 31, yup, 03-19-2017, 32, nice, john

so how would my select statement look like if i wanted to display data like this? 所以如果我想显示这样的数据,我的select语句会如何?

id | name       | date       | qty | note | admin
-------------------------------------------------
1  | something  | 03-17-2017 | 20  | wow  | greg
2  | nothing    | 03-17-2017 | 25  | hmm  | dave
2  | nothing    | 03-18-2017 | 26  | ok   | dave 
3  | everything | 03-17-2017 | 30  | cool | john
3  | everything | 03-18-2017 | 31  | yup  | john
3  | everything | 03-19-2017 | 32  | nice | john

so if there are no values for the #2 & #3 columns, then the records displays once. 因此,如果#2和#3列没有值,那么记录将显示一次。 if there are no values in the #3 columns, then the record displays twice. 如果#3列中没有值,则记录显示两次。 if the #1, #2, and #3 columns have data, then the record displays as 3 rows. 如果#1,#2和#3列中有数据,则记录显示为3行。 when the record shows as 2 or more rows, each row should have some same values and some different values. 当记录显示​​为两行或更多行时,每行应具有相同的值和不同的值。

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for jobs
-- ----------------------------
DROP TABLE IF EXISTS `jobs`;
CREATE TABLE `jobs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `admin` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `date1` varchar(50) NOT NULL,
  `note1` varchar(255) NOT NULL,
  `qty1` varchar(50) NOT NULL,
  `date2` varchar(50) NOT NULL,
  `note2` varchar(255) NOT NULL,
  `qty2` varchar(50) NOT NULL,
  `date3` varchar(50) NOT NULL,
  `note3` varchar(255) NOT NULL,
  `qty3` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of jobs
-- ----------------------------
INSERT INTO `jobs` VALUES ('1', 'greg', 'something', '03-17-2017', 'wow', '20', '', '', '', '', '', '');
INSERT INTO `jobs` VALUES ('2', 'dave', 'nothing', '03-17-2017', 'hmm', '25', '03-18-2017', '26', 'ok', '', '', '');
INSERT INTO `jobs` VALUES ('3', 'john', 'everything', '03-17-2017', 'cool', '30', '03-18-2017', '31', 'yup', '03-19-2017', '32', 'nice');
SET FOREIGN_KEY_CHECKS=1;

Try a query like this, but in your post the create is not correct and i am nearly sure that the insert also changed the fields. 尝试这样的查询,但是在您的帖子中创建不正确,我几乎可以确定插入内容也更改了字段。

SELECT id, `name`, date1, qty,note,admin
FROM(

    SELECT id, 1 AS subid, `NAME`, date1 AS date1 , qty1 AS qty, note1 AS note , admin FROM jobs
    UNION ALL
    SELECT id, 2 AS subid, `name`, date2 AS date1 , qty2 AS qty, note2 AS note , admin FROM jobs WHERE date2 <>''
    UNION ALL
    SELECT id, 3 AS subid, `name`, date3 AS date1 , qty3 AS qty, note3 AS note , admin FROM jobs WHERE date3 <> ''

    ) AS tabs
ORDER BY id,subid;

It would probably be better to have multiple Primary Keys for the table. 该表具有多个主键可能会更好。 Such as PRIMARY KEY(id, date) 如PRIMARY KEY(id,date)

CREATE TABLE notes (
  id INT NOT NULL AUTO_INCREMENT,
  name CHAR(30),
  date INT,
  qty INT,
  note CHAR(50),
  admin CHAR(10),
  PRIMARY KEY(id, date)
);

您可以通过使用UNION ALL来实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM