To select something from the data base we could use:
SELECT * FROM tableName where name="Ed"
But what if I need to select something from a given array, eg:
SELECT * FROM ("Bob","Sam","Ed") where name="Ed"
Is it possible?
Yes it is possible:
http://sqlfiddle.com/#!9/9eecb7d/64737
SELECT t.* FROM
(SELECT "Bob" name UNION SELECT "Sam" UNION SELECT "Ed") t
WHERE t.name="Ed"
But it has almost no sense. Because if you set all data as constant static values you can just:
SELECT "Ed"
there is no reason even to call mysql :-)
You can try with
SELECT * FROM (
select "Bob" as name
from dual
union
select "Sam" as name
from dual
union
select "Ed"as name
from dual ) as t
where t.name="Ed";
Related
How can I set a list of values by MYSQL in one column like the following query:
SELECT (100005355, 100001548, 100000672, 100001230, 100003382, 100009937, 100003778, 100002486, 100003301, 100005056, 100003435, 100003666, 100002761, 100008818, 100008341, 100003438, 100000581, 100009800, 100007165) AS ID;
result needs to be like this list
You can do it with UNION ALL:
SELECT 100005355 AS ID UNION ALL
SELECT 100001548 UNION ALL
SELECT 100000672 UNION ALL
SELECT .....
If you want to create a new table:
CREATE TABLE tablename AS
SELECT 100005355 AS ID UNION ALL
SELECT 100001548 UNION ALL
SELECT 100000672 UNION ALL
SELECT .....
In MySql 8.0+ you can use the VALUES statement with ROW constructor:
WITH cte(ID) AS (VALUES ROW(100005355), ROW(100001548), ROW(100000672))
SELECT * FROM cte
See the demo.
if you want just show the value you could try using group_concat
select group_concat(id) as my_col
from your_table
or use the saperator you prefer
select group_concat(id separator ', ') as my_col
from your_table
Did you tried this
select CONCAT_WS("", 100005355, 100001548, 100000672, 100001230, 100003382, 100009937, 100003778, 100002486, 100003301, 100005056, 100003435, 100003666, 100002761, 100008818, 100008341, 100003438, 100000581, 100009800, 100007165 ) AS ID;
I have a table with two columns, one column named user, one json column named js that looks like this:
{"1":{"partner_id":54,"provider_id":13},
"2":{"partner_id":56,"provider_id":8},
"3":{"partner_id":2719,"provider_id":274}}
I want to select all 'provider_id' in one column/row.So it should look like this:
user| provider_ids
0001| 13,8,274
0002| 21,36,57,12
How can I do this? Thanks in advance!
Your provided json format is not so easy to work with.
Crated table for test purposes:
create table json_test as
select '0001' as usr, '{"1":{"partner_id":54,"provider_id":13},
"2":{"partner_id":56,"provider_id":8},
"3":{"partner_id":2719,"provider_id":274}}'
as json_text
union all
select '0002' as usr, '{"1":{"partner_id":54,"provider_id":21},
"2":{"partner_id":56,"provider_id":36},
"2":{"partner_id":56,"provider_id":57},
"3":{"partner_id":2719,"provider_id":12}}'
as json_text;
Query to return results:
with NS AS (
select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
)
select usr,
listagg(trim(TRIM(split_part(SPLIT_PART(js.json_text, '},', NS.n),'"provider_id":',2)),'}'),',') within group(order by null) AS t
from NS
join json_test js ON true and NS.n <= REGEXP_COUNT(js.json_text, '\\},') + 1
group by usr;
Notes:
1) do not name column "user" as it is reserved keyword
2) add as many dummy rows in NS subquery as there is maximum of json provider records
3) Yes, I know, this isn't very readable SQL :D
In sql i need to get my own values.
For example :
Select House from dual ( here i need to return H ) from dual union all
select Build from dual ( here i need to return B) ...
I tired with this but i dont get it as I want
select '18776' as instanceid from dual union all
select '18775' as instanceid from dual
You can hardcode it like
Select 'H' from dual
union all
select 'B' from dual
#mikrimouse simply just write hard code in a query like:
select 'H' from dual
union all
select 'B' from dual
Looks like you're coming from an Oracle background. Oracle is unique in requiring a FROM clause among the DBMSes I've used (SQL Server, Oracle, MySQL).
You should be good just having:
select 'H'
union all
select 'B'
How do I select like this:
SELECT * FROM
(SELECT 11 AS Value UNION
SELECT 24 AS Value UNION
SELECT 53 AS Value UNION
SELECT 124 AS Value UNION
SELECT 2215 AS Value) AS ValueTable
This query give me a perfect result, just this query is ugly.
How create this just nicely such as:
select 11,24...
select arrray(22,24...)
These don't works just examples.
Thx
Your query is correct. The only thing I can do to make it pretty is remove all the field name declaration after the first one
SELECT *
FROM (SELECT 11 AS Value UNION
SELECT 24 UNION
SELECT 53 UNION
SELECT 124 UNION
SELECT 2215) AS ValueTable
There is no option like this you propose, because it is usual to have separate data from logic:
/* Data */
CREATE TABLE ValueTable (Value INT);
INSERT INTO ValueTable (Value) VALUES (11), (24), (53), (124), (2215);
/* Logical */
SELECT * FROM ValueTable;
is it possible in an SQL query to add UNION after order by 1
SELECT * FROM table1 WHERE etc='1' ORDER BY 11;
can we add a union select query beside 11 to be like this ?
SELECT * FROM table1 WHERE etc='1' ORDER BY 11 UNION select etc etc etc ...;
In MySQL, you can enclose the order by clause within a subquery and union the results of multiple subqueries; something like this:
SELECT * FROM (SELECT * FROM table1 WHERE etc='1' ORDER BY 11) sq1
UNION ALL
SELECT * FROM (SELECT * FROM table2 WHERE etc='2' ORDER BY 12) sq2
...
Maybe something like this:
SELECT
*,
11 AS orderby
FROM
table1
WHERE
etc='1'
UNION
select
*,
10 AS orderby
FROM
table2
ORDER BY
orderby
You can use like this:
SELECT * FROM table1 WHERE etc='1'
union
SELECT * FROM table1 WHERE etc='1'
ORDER BY any_column
You can only use ORDER BY at the end of the query after the last union. Basically all SELECTs are done first, then the entire result set is ordered.