Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Select from list of values

Tags:

sql

mysql

I was wondering if I could select given values from a list and populate rows? For example, SELECT 1 as one, 2 as two, 3 as three will populate columns:

one    | two    | three
------------------------
1      | 2      | 3

I'm looking for a script that populates rows, something like:

values
-------
1
2
3
4

Thanks!

like image 558
bodruk Avatar asked Sep 23 '14 16:09

bodruk


People also ask

How do I SELECT a list in MySQL?

mysql> select *from ListOfValues where Age IN(20,21,23,25,26,27,28) -> order by field(Age,20,21,23,25,26,27,28); The following is the output.

What is query for list in MySQL?

A list of commonly used MySQL queries to create database, use database, create table, insert record, update record, delete record, select record, truncate table and drop table are given below.

How do I group data in MySQL?

The MySQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

What does := mean in MySQL?

Description. := Assign a value. = Assign a value (as part of a SET statement, or as part of the SET clause in an UPDATE statement)


1 Answers

you can union each one if you want like so

SELECT 1 AS numbers
UNION SELECT 2
UNION SELECT 3

a much simpler way to do something like this would be to make a table with an auto incremented id... insert into another column in the table an empty string... then just select the auto incremented id

CREATE TEMPORARY TABLE tmp (
    id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    val  varchar(1) 
);
INSERT INTO tmp (val)
values
(""),
(""),
(""),
(""),
(""),
(""),
(""),
(""),
(""),
("");
select id from tmp;

DEMO

like image 84
John Ruddell Avatar answered Sep 20 '22 19:09

John Ruddell