Mysql: How to get custom resultset as select statement output - mysql

I have below Mysql query -
Select ('GOOGLE.COM',
'MSN.COM',
'YAHOO.COM',
'YOUTUBE.COM',
'BING.COM',
'FACEBOOK.COM',
'LIVE.COM',
'MICROSOFT.COM',
'WIKIPEDIA.ORG',
'LINKEDIN.COM') as domain from dual;
I'm expecting the below resultset.
'GOOGLE.COM'
'MSN.COM'
'YAHOO.COM'
'YOUTUBE.COM'
'BING.COM'
'FACEBOOK.COM'
'LIVE.COM'
'MICROSOFT.COM'
'WIKIPEDIA.ORG'
'LINKEDIN.COM'
But I'm getting error Operand should contain 1 column(s).
How can I fix my query so I can get correct results?

If you want one record per value, you can use union all:
select 'GOOGLE.COM' domain
union all select 'MSN.COM'
union all select 'YAHOO.COM'
union all select 'YOUTUBE.COM'
union all select 'BING.COM'
union all select 'FACEBOOK.COM'
union all select 'LIVE.COM'
union all select 'MICROSOFT.COM'
union all select 'WIKIPEDIA.ORG'
union all select 'LINKEDIN.COM'
In other RDMBS, such as Postgres or SQL Server, this would have been as simple as:
SELECT domain
FROM ( VALUES
('GOOGLE.COM'),
('MSN.COM'),
('YAHOO.COM'),
('YOUTUBE.COM'),
('BING.COM'),
('FACEBOOK.COM'),
('LIVE.COM'),
('MICROSOFT.COM'),
('WIKIPEDIA.ORG'),
('LINKEDIN.COM')
) AS t(domain);
But MySQL does not support this syntax. A workaround is to to create a table, that you can then link in your queries:
CREATE TABLE tmp (domain VARCHAR(50));
INSERT INTO tmp(domain) VALUES
('GOOGLE.COM'),
('MSN.COM'),
('YAHOO.COM'),
('YOUTUBE.COM'),
('BING.COM'),
('FACEBOOK.COM'),
('LIVE.COM'),
('MICROSOFT.COM'),
('WIKIPEDIA.ORG'),
('LINKEDIN.COM')
;

Related

Select IN with an array with duplicate value on it

I am trying to make a request where I select from an array of value using the IN, but inside this array, if I have the same value twice, I'd like the request to return the result twice.
To clarify, here is an example:
select id_exo, count(id_exo) FROM BLOC WHERE id_seance IN (10,10) group by id_exo
So inside the IN, I put 2 times the value 10.
Here is the result:
id_exo
count(id_exo)
60
1
82
1
But in count, I'd like to have the number 2 since I have put twice 10 inside my IN.
How can I achieve that?
SELECT id_exo, COUNT(id_exo)
FROM bloc
JOIN (SELECT 10 id_seance
UNION ALL
SELECT 10) val USING (id_seance)
GROUP BY id_exo
Prior to MySQL 8.0 you can join with a sub select:
select * from BLOC as b
inner join (
select 1 as 'id', 10 as 'value'
union
select 2,10
union
select 3,10) as myValues on myValues.value = b.id_seance
You need the id column as the UNION statement removes duplicate rows
If you are lucky enough to have MySQL 8.0 look at the VALUES statement
https://dev.mysql.com/doc/refman/8.0/en/values.html
Here you should instead be able to join with something like
VALUES ROW(10), ROW(10), ROW(10)

How to add more values into a declared variable?

I wanted to insert a values into a declared variable.
This is how it is so far :
INSERT INTO DP_Transaction(Trans_Reference, Trans_Action, Trans_Name,
Trans_Date, Trans_Comment)
VALUES(#formId, 'Cancelled by', 'Workflow Manager', '2021-03-29 09:52:28.257', 'test')
And the values I want to add inside #formid is something like below:
'12323985',
'39864345',
'89426596',
'97070302',
'56746838'
And my expected result is all the values inside the statement will be insert into these #formId rows:
'12323985',
'39864345',
'89426596',
'97070302',
'56746838'
Can anyone help?
You could place the form ID values into a subquery and then rephrase the insert as an INSERT INTO ... SELECT:
INSERT INTO DP_Transaction (Trans_Reference, Trans_Action,
Trans_Name, Trans_Date, Trans_Comment)
SELECT
formid,
'Cancelled by',
'Workflow Manager',
'2021-03-29 09:52:28.257',
'test'
FROM
(
SELECT '12323985' AS formid UNION ALL
SELECT '39864345' UNION ALL
SELECT '89426596' UNION ALL
SELECT '97070302' UNION ALL
SELECT '56746838'
) t;
If you were using a different database, e.g. SQL Server, then you could have used APPLY along with a string splitting function. But this is not an option in MySQL to my knowledge.

Not getting count of different string - MySql

I have one query as given below,
select device_id,CAST(device_dtt_st as date),count(*) as g,'' as s,'' as m
from event_data_170309
where device_id ='8D-15-DB'and raw_data like %GPRS%'
group by CAST(device_dtt_st as date)
union
select device_id,CAST(device_dtt_st as date),'' as g,count(*) as s,'' as m
from event_data_170309
where device_id ='8D-15-DB' and raw_data like '%SMS%'
group by CAST(device_dtt_st as date)
union
select device_id,CAST(device_dtt_st as date),'' as g,'' as s,count(*) as m
from event_data_170309
where device_id ='8D-15-DB'and !(raw_data like '%SMS%' or raw_data like '%GPRS%')
group by CAST(device_dtt_st as date);
and I got output as in two different row, but I want count in only one row.
see the below scenario,
Union will return multiple rows only.
You will need to wrap all these queries with another query and then count it.
ex.
select count(param), sum(param), param
from
(
select param as param, count(param)
union
another query with same column output
union
yet another query with same column output
) as childQuery
group by childQuery.param
EDIT
Added a aggregated function, whichever you want to use.
EDIT2
SELECT
DEVICE_ID,
DATE,
SUM(IF(DATA LIKE %SMS%,1,0)) AS TOTAL_SMS,
SUM(IF(DATA LIKE %GPRS%,1,0)) AS TOTAL_GPRS,
SUM(IF(DATA NOT LIKE %GPRS% AND DATA NOT LIKE %SMS%,1,0)) AS TOTAL_OTHER,
FROM
YOUR_TABLE T
GROUP BY
T.DATE
ABove query will work for your desired output

Using UNION across databases | SQL

I wan't to get informations which are safed in 2 databases with only one mysql query, using the UNION statement.
The first to selects are for the database sport the other from dewiki.
If i send this request, there is no error, but there are up to 29 BLOB-results.
which if i open it are filled with errors like SELECT page_title
FROM dewiki.page: Database '' does not exist.
This is my sql-statement:
(SELECT teamName FROM sport.leagueTeams WHERE teamName LIKE '%werder%')
UNION
(SELECT leagueAlias FROM sport.leagueAlias WHERE leagueAlias LIKE '%bundesliga%')
UNION
(SELECT title FROM dewiki.cachedArticles WHERE title LIKE '%werder%')
UNION
(SELECT page_title FROM dewiki.page WHERE page_title LIKE '%werder%')
Just remove all parenthesis!!!
They break the correct syntax as they transform each individual select into a subquery. Union joins two or more querys, not subquerys

Get a list of ids not present in a table

I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.