0

How can I do somethig like this in MYSQL?

SELECT MyFunction(x) where x in (1,2,7,20,30);

Seems like a simple query should exist for this and in Oracle I can do it easily using the Dual table but I don't see a trick to do this in MySQL. NOTE that in my case 1,2,7,20,30 do NOT come from a table (in that cast it would be easy, just a simple join). In my situation I will be generating the query ( in particular the integer values) in C# and then making the DB Call.

Any Ideas?

shelbypereira
  • 2,097
  • 3
  • 27
  • 50

2 Answers2

1

Example output would help but maybe something like this....

Select myFunction(X)
FROM (Select 1 UNION Select 2 union select 7 union select 20 union select 30)\

Alternate:

--PSEUDO...

Select myFunction(X)
FROM (Generate Table containing values Min to max of range passing in) 
where Value in (your list);

Something like: Select a sequence between two numbers on MySQL

Community
  • 1
  • 1
xQbert
  • 34,733
  • 2
  • 41
  • 62
  • this works for me as in my immediate situation I will have max 10 integer values! I guess there is no cleaner solution for larger sets say 50 values? A way which avoids the somewhat clumsy "union select" repetition 50 times? – shelbypereira Apr 03 '16 at 15:22
  • Since the values are provided and not pattern based, we can't loop to generate them. the Clumsy union select is all I can think of here.... Unless... We know min max, then we could loop and generate a set say 1-1000 and then simply say where in... but I'm not sure that's any cleaner. – xQbert Apr 03 '16 at 15:34
1

union is a way to do that.

SELECT (x + 100) 
FROM (select 1 as x union select 2 union select 7 union select 20 union select 30) t;

Output:

mysql> SELECT (x + 100)
    -> FROM (select 1 as x union select 2 union select 7 union select 20 union select 30) t;
+-----------+
| (x + 100) |
+-----------+
|       101 |
|       102 |
|       107 |
|       120 |
|       130 |
+-----------+
5 rows in set (0.00 sec)
Dylan Su
  • 5,975
  • 1
  • 16
  • 25