MYSQL - calculated column aliases? - mysql

I want to have a SELECT statement name columns based on other column values.
Let's say I have a table with column names like q_1, q_2 and other columns like q_1_name and q_2_name
Right now we are doing something like
SELECT SUM(q_1), SUM(q_2) from mytable;
I'd like to get a result set with the columns named for the values in q_1_name and q_2_name
SELECT SUM(q_1) as (q_1_name), SUM(q_2) as (q_2_name) from mytable;
Any chance you know a way to do this?

You can use a simply alias AS
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable;
or using a subselect
select t.q_1_name, t.q_2_name
from (
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable
) t;

Related

select all columns, simultaneously doing a ifnull on one

I want to be able to do a select on all columns, displaying a 0 (for a few of them) if null, without having to write each of the columns' names in the statement.
All I could think of is something like this:
SELECT *, IFNULL(`nullable_col1`, 0) FROM `my_table`;
What's the right way to do this?
No there is no way. You have to use the IFNULL function on each column which you want to have the value for.
One thing which you can do is that, you can simply select the value for all the columns which are not NULL(but I am not sure if that is what you want)
SELECT * FROM my_table WHERE (nullable_col1 AND nullable_col2 AND nullable_col2) IS NOT NULL
So this will select only columns which are not NULL.

Is there any way in MySQL, to create a single select statement where the select column is the result of another query?

My first select statement would be like below,
SELECT m.col_name
, m.col_alias
FROM <table_name> m
WHERE m.exportable LIKE '%Y%'
And I'm trying to create a second select query with the data that I'm receiving from the first statement, like below
SELECT tabella.id alias_1
, tabella.value alias_2
, **“list of col_name result of the previous query”
FROM <another_table> tabella
WHERE tabella.metadata_id = 'CI_INDEX||CI_01'*
Thanks in advance.
Try the Subquery.
You will find an example in the links.
If the tables have relation between them then go for Join
if the tables do not have relation you can use the following format.
select table1.col1,table2.col1 from table1, table2
where table1.colx like '%exp%' and table2.coly like '%exp2%'

Is it possible to select data in mysql matching values from any of the existing columns?

Is there a way to select data from any of the existing columns in a databse?
Something like SELECT * FROM mytable WHERE AnyOfTheTableColumns = Something
No, column name can't be variable! You must define your query with explicit definition of columns as;
SELECT * FROM mytable
WHERE column1=Something OR column2=Something OR column3=Something

How to get all columns from a SELECT DISTINCT query?

I'm using PDO with MySQL.
I want to select all rows from a given table with distinct values in a given column, but SELECT DISTINCT column_name FROM table returns the rows with only that column_name. Therefore I can't access the other row's columns.
I've been searching for answers and it looks like SELECT DISTINCT column_name FROM table is supposed to return all the rows with distinct values inside column_name with all the row's columns. However, I only get the column I want distinc'ed:
Array
(
[image] => leather_helmet.jpg
// there are supposed to be more fields here...
)
May this be a PDO's bug or am I doing something wrong?
Thanks in advance! :)
If you want only 1 column distinct you have to think of which record you want for the other columns. For instance if you like the min id record for the distinct column then you can do
SELECT *
FROM armor_unsealed
WHERE id IN
(
SELECT min(id)
FROM armor_unsealed
WHERE piece=:piece
GROUP BY image
)'

How to return NULL when result is empty?

I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0