SELECT `t1`.`number`, COUNT( * )
FROM (
SELECT `id`, `number1` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number2` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number3` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number4` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number5` AS `number`
FROM `table`
) AS `t1`
JOIN (
SELECT `id`
FROM `table`
ORDER BY `id` DESC
LIMIT 100
) AS `t2` ON `t2`.`id` = `t1`.`id`
GROUP BY `t1`.`number`
ORDER BY COUNT( * ) DESC
SELECT `number` , COUNT( * )
FROM (
SELECT `id`, `number1` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number2` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number3` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number4` AS `number`
FROM `table`
UNION ALL SELECT `id`, `number5` AS `number`
FROM `table`
) AS `temp`
WHERE `id` IN (
SELECT `id`
FROM `table`
ORDER BY `id` DESC
LIMIT 100
)
GROUP BY `number`
ORDER BY COUNT( * ) DESC
If an ENUM column is declared to permit NULL, the NULL value is a valid value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 62455
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)
mysql> create database `test`;
Query OK, 1 row affected (0,00 sec)
mysql> use test;
Database changed
mysql> create table `test` (`id` INT, `enum` ENUM ('one', 'two', 'three') NOT NULL);
Query OK, 0 rows affected (0,07 sec)
mysql> show create table `test`;
+-------+-------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`enum` enum('one','two','three') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)
mysql> insert into `test` values (1, 'one');
Query OK, 1 row affected (0,01 sec)
mysql> insert into `test` values (1, NULL);
ERROR 1048 (23000): Column 'enum' cannot be null
mysql> insert into `test` values (1);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
SELECT COUNT(*) + 1 AS `position`
FROM `people` AS `p1`
JOIN `people` AS `p2` ON `p1`.`id` = :user_id
AND (`p2`.`votes_count` > `p1`.`votes_count`
OR (`p2`.`votes_count` = `p1`.`votes_count`
AND `p2`.`name` < `p1.`name`))
var_dump($f);