Transform an int result to an option of strings in MySQL - mysql

I have a database that contains these values:
ID Value
1 Mail
2 Portal
3 Terrain
And in my program, I want to make a select, so when I pick a number, it shows the 'value' of that number. Something like:
SELECT ID as Mail, Portal, Terrain
FROM TABLE

You may be looking for conditional aggregation:
select max(case when id = 1 then value end) as mail,
max(case when id = 2 then portal end) as portal,
max(case when id = 3 then terrain end) as terrain
from ;

Make sure you're giving the ID by a textbox or something in your program
Select Value from TABLE where ID = #ID

Related

How to pivot table in MySql?

I have a table illustrated on the attached screen:
And data is presented:
As you can see it contains all fields from lines (value, column_id - is field type). Each field has line_index and column_index of position and column_id.
I want to get back all lines with all field names like:
line_index column_id_1 column_name1 column_id_2 column_name2 column_id_3 column_name3
0 1 Age 2 Vasile 3 NY
More simply I need to build all fields(rows) to columns then to lines back.
Should I use Pivot and is it possible in MySQL?
Link to sqlfiddle
You will need to group the results by line_index and conditionally transform rows to columns.
SELECT l.line_index,
MAX(CASE WHEN ct.column_name = "NAME" THEN l.value ELSE NULL END) AS "NAME" ,
MAX(CASE WHEN ct.column_name = "AGE" THEN l.value ELSE NULL END) AS "AGE" ,
MAX(CASE WHEN ct.column_name = "ZUP" THEN l.value ELSE NULL END) AS "ZUP"
FROM columns_types ct
LEFT JOIN `lines` l ON l.column_id = ct.column_id
GROUP BY l.line_index;
Try it here. Also I did some changes in your schema which I felt didn't impact the data stored in the tables. You were adding redundant rows(perhaps) in your columns_types table. And by looking at the query, you know that you will have to build a MAX(CASE statement for each column, so its best if you first fetch it and then build the final query in some programming language.

MySQL count different value in same table

I am working on a database right now, and I am trying to select some special data.
so the table looks like this.
name title type
Type is including two different value, "book" and "paper".
And this is the result I would like to get
name book paper
person A 0 1
person B 1 2
person C 0 5
What is the best way to write the query it in MySQL.
You may use conditional aggregation:
SELECT
name,
SUM(CASE WHEN type = 'book' THEN 1 ELSE 0 END) AS book,
SUM(CASE WHEN type = 'paper' THEN 1 ELSE 0 END) AS paper
FROM yourTable
GROUP BY
name;

Column has Multiple Values Used as Identifiers for Next Column

MySQL column has multiple values used as identifiers for the next column. Table Structure:
id (key), occurrence_id, name, value
The name column values then correspond to the values column. How can I display this information in one view?
Currently, it looks like this:
occurrence_id name value
1576 Attempts 1
1576 ClientIP "94.xxx.xxx.xxx"
1576 UserAgent ""
1576 CurrentUserID 0<
I want to make a view that will look like this:
occurrence_id Attempts Client IP CurrentUserID
1576 1 "94.xxx.xxx.xxx" 0
2009 30 "68.111.xxx.xxx" 0
One method is using conditional aggregation:
select occurrence_id,
max(case when name = 'Attempts' then value end) as Attempts,
max(case when name = 'ClientIP' then value end) as ClientIP,
max(case when name = 'UserAgent' then value end) as UserAgent,
max(case when name = 'CurrentUserID' then value end) as CurrentUserID
from table t
group by occurrence_id;

Rails 4 get column names in raw activerecord query

I have some code that looks like the following:
query = <<-EOF
select player_id, first_name, last_name,
max(case when site_id = 1 then salary end) fd_salary,
max(case when site_id = 2 then salary end) dd_salary,
max(case when site_id = 3 then salary end) ss_salary,
max(case when site_id = 4 then salary end) ds_salary,
max(case when site_id = 7 then salary end) dk_salary,
max(case when site_id = 8 then salary end) elite_salary
from player_salaries ps
where ps.gamedate = '2014-05-25'
and sport_id = #{$MLB_SPORT_ID}
group by player_id
EOF
salaries = PlayerSalary.connection.execute(query)
The problem is salaries in this case comes back as an array with values. These query is a bit complex and the names I'm using such as fd_salary, dd_salary and so forth aren't physical attributes in the PlayerSalary model. There's no way to do something like salaries.first.fd_salary. Is there a way to change the above in Rails 4 to get it access values by column name?
You could use find_by_sql for this:
find_by_sql(sql, binds = [])
Executes a custom SQL query against your database and returns all the results. The results will be returned as an array with columns requested encapsulated as attributes of the model you call this method from. If you call Product.find_by_sql then the results will be returned in a Product object with the attributes you specified in the SQL query.
So if you did:
salaries = PlayerSalary.find_by_sql(query)
then you could say things like salaries.first.fd_salary. Just don't try to use columns that weren't in your query or try to change the returned PlayerSalary objects and expect anything useful to happen.
If you want to retrieve your records as a hash, with column names you can use mysql2 adapter directly with:
ActiveRecord::Base.connection.instance_variable_get('#connection').query("SELECT * FROM `users`", as: :hash).first
I was searching for another way to do it but had no luck.

mysql - performing addition, subtraction etc on two rows

I have a following Mysql table storing meter readings of different power stations.
Date, station_name, reading
2013-05-06, ABC, 102
2013-05-06, PQR, 122
I want a SQL query with following result for a particular date.
Date, ABC, PQR, ABC-PQR
2013-05-06,102,122,-20
You could use CASE statements:
SELECT Date
, SUM(CASE WHEN station_name = 'ABC' THEN reading ELSE 0 END) as ABC
, SUM(CASE WHEN station_name = 'PQR' THEN reading ELSE 0 END) as PQR
, SUM(CASE WHEN station_name = 'ABC' THEN reading ELSE 0 END) - SUM(CASE WHEN station_name = 'PQR' THEN reading ELSE 0 END) as 'ABC-PQR'
FROM table
WHERE Date = '20130506'
GROUP BY Date
You can search for MySQL PIVOT to find out other methods people use.
I believe that it is not possible to do dynamic column based on value of row. I believe you should do it in application-layer rather than database-layer.
See this post: mysql select dynamic row values as column names, another column as value.