2
I am creating a system where the user informs the name and time of birth. I would like to know whether the following question can be resolved: I have the following table:
Having the name and time of birth, the system administrator informs the priority of each record, being:
1 = high priority, 2 = high priority, 3 = low priority, 4 = no high priority. Then I want these records to be displayed in priority order, and 2 in 2. Example: 2 priority records 1... then 2 priority records 2... 2 priority records 3...etc No, it needs to be in the same select, it could use more than one select, but as long as there are records, I need to list 2 in 2, without repeating them. Would look like this:
I’m using Mysql and Php. Does anyone have any idea how I can do this? Thank you!
Follow the mysql table:
CREATE TABLE `tbl_nasc_users` (
`id` int(111) NOT NULL,
`nome` varchar(30) NOT NULL,
`hr_nasc` time NOT NULL,
`prioridade` int(9) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `tbl_nasc_users` (`id`, `nome`, `hr_nasc`, `prioridade`) VALUES
(1, 'Pedro', '17:34:40', 1),
(2, 'João', '17:23:18', 2),
(3, 'Marcos', '17:56:39', 2),
(4, 'Vinicius', '18:12:48', 1),
(5, 'Miguel', '18:36:53', 3),
(6, 'Bruno', '18:30:10', 4),
(7, 'Felipe', '18:48:42', 3),
(8, 'Antonio', '19:34:40', 1),
(9, 'Victor', '19:23:18', 2),
(10, 'Ronaldo', '19:56:39', 2),
(11, 'Ricardo', '20:12:48', 1),
(12, 'Teodoro', '20:36:53', 3),
(13, 'Gabriel', '21:30:10', 4),
(14, 'Patrick', '22:48:42', 3);
ALTER TABLE `tbl_nasc_users`
ADD PRIMARY KEY (`id`);
ALTER TABLE `tbl_nasc_users`
MODIFY `id` int(111) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;
If the ordering should be from the smallest to the largest, why do numbers appear smaller than 4 at the end of the list?
– Woss
Yago, in these cases it will not be a very natural ordination, because the "Pedro" and the "Vinicios" may appear at the beginning as well as the "Antonio" and the "Ricardo" may appear. Do you have any other criteria in this order?
– jlHertel
If he orders from the smallest to the largest the ordering will look like this: 1,1,1,1,1,1,2,2,3,3,4,5,5,5,5 As you want different, you need to create a criterion to be used.
– Reginaldo Rigo
So, guys, it’s a system where the user reports the name and time of birth. After that I inform what the priority of each record, being, 1 = very priority, 2 = priority, 3 = little priority and 4 = no priority. Then I want these records to be displayed in priority order, and 2 in 2. Example: 2 priority records 1... after 2 priority records 2... 2 priority records 3...etc No, it needs to be in the same select, could use more than one select, but while there are records, I need to list 2 in 2, without repeating.
– Yago Lima
I can’t think of a criterion that meets that need. I thought of creating a select for each priority, using LIMIT 2. However, how do I keep the result following sequence?
– Yago Lima
The question is: why, in your example, do records appear with "very high priority" after "non-priority" records, since you want ascending order? This part is not clear. Whenever you reach priority 4 the numbering should return to 1?
– Woss
Exactly Anderson! When you get to the 4 it returns to display the priority 1. The idea is this! If there are still records with priority 1 it will continue showing 2 in 2 always
– Yago Lima
Maybe
union
can help with a subquery - but I’m not 100% sure.– Papa Charlie
Papa Charlie, I tried to use the following: (SELECT * FROM
tabela
WHERE priority = 1 ORDER by hora_nasc ASC LIMIT 2) UNION (SELECT * FROMtabela
WHERE priority = 2 ORDER by hora_nasc ASC LIMIT 2) UNION (SELECT * FROMtabela
WHERE priority = 3 ORDER by hora_nasc ASC LIMIT 2) UNION (SELECT * FROMtabela
WHERE priority = 4 ORDER by hora_nasc ASC LIMIT 2) .– Yago Lima
@Yagolima, can you make the table data available as a single table? They are in image format and so it is difficult to copy them to simulate tests.
– jlHertel
@jlHertel posted! Thank you
– Yago Lima