Link two different if statements to same value (mysql query) - mysql

Let's say that I have two if statements on two different columns that determine that value that I wanna display in a third virtual column. Something like:
if(column1 = 'activated'){
return 15;
}
if(colunm2 = 1){
return 'enabled';
}
I want to display the return values in a virtual column called let's say output in a select query. Something like:
SELECT IF(column1 = 'activated', 15, [what do i put here...]),
IF(column2 = 1, 'enabled', [what do i put here]) ..... AS consent
Obviously the above query doesn't work cause I can't separate my ifs with a comma since they have to be bound to the same column called (output).
How can I achieve this?
Thanks for any help.

You seem to want a case expression:
select (case when column1 = 'activated' then '15'
when column2 = 1 then 'enabled'
end) as consent

Related

Need to change SQL query for a temporary result

I need a temporary result for just a query. If substring condition true, I want to lower its car_performance value by %10 (car_performance = car_performance*0.9;) and compare this substring true cars against others BUT ONLY FOR THE QUERY, I don't want to change the real data in SQL database.
So I thought I need to get this data to a new temporary table with lowered car_performance values but I can't figure out how to write it.
SELECT *
FROM car_list
IF SUBSTRING(car_model, 9, 1) = '3'`
car_performance = car_performance*0.9;
ORDER BY car_performance ???
I'm not sure I did correctly understand you. Correct me if I am wrong. Do you want to select all cars that respect condition SUBSTRING(car_model,9,1) = 3 and print their performance like 0.9*performance? If so, you can use this statement:
SELECT car_model, car_performance*0.9 as car_performance
FROM car_list
WHERE SUBSTRING(car_model, 9, 1) = '3'
I also recommend you to take a SQL course. It is easier and faster to understand after you learn the basics. There are a lot of great free resources online.
I suppose you need a comparison for whole set of cars depending on a condition for some of them, and leaving the rest as they are. If so, CASE WHEN ... THEN .. ELSE ... conditional statement will do the trick such as
SELECT CASE WHEN SUBSTRING(car_model, 9, 1) = '3'
THEN car_performance * .9
ELSE car_performance
END AS car_performance
FROM car_list
ORDER BY car_performance

Use ValueA when JOIN returns a row, otherwise ValueB (like a default)

I have four tables for a form-builder in my databse.
fields (fieldID(PK), typeID, fieldName, ...) - This table is a row by row list of all fields to be in the form
fields_types (typeID(PK), htmlType, ...) - This is a table that links fields to html types (and other settings)
fields_meta (FieldMetaID(PK), FieldID, mName, mValue) - Additional settings for fields, but more specific. A textarea field might have a height attribute, but almost no other field would use that.
fields_tyeps_meta (TypeMetaID(PK), typeID, tmName, tmValue) - Defines what extraneous settings a field can have, and also supplies default values if it's not explicitly set)
So my Query currently looks something like this
SELECT *
FROM Fields F
JOIN Field_Types FT
on FT.FieldID = F.FieldID
LEFT
JOIN Field_Meta FM
on FM.FieldID = F.FieldID
I was wondering if there's a way to join Fields_Types_Meta so that when the row's JOIN to Fields_Meta doesn't return a row (no mValue), it returns tmValue
I realize I can use something like (CASE WHEN mValue = "" THEN tmValue ELSE mValue END) AS UseValue, but I might have fields where I want to allow the value to be set to empty.
Edit: I could probably do something with a subquery and COUNT, using a CASE decision based on that. It might not be the healthiest performance-wise, but this query runs and caches itself til server restart, or until it's told to run again (updates to form design)
It looks like you just want ¢oalesce():
coalesce(FM.mValue, FT.tmValue) as UseValue
When FM.mValue is null, coalesce() returns FT.tmValue instead.
If you have null values in FM that you want to preserve in the result set, then use a case expression instead:
case when FM.FieldID IS NULL THEN FT.tmValue ELSE FM.mValue END as UseValue
This phrases as: when the left join did find a match in FM, use mValue from that row (even if it is null), else use FT.tmValue.

How to count all records if use alias in select query?

I use Sphinx with Yii2 and need to query with filter by jSON field.
$query = new \yii\sphinx\Query();
$query->from('announcements');
$query->addSelect("*");
$query->addSelect(new Expression("IN(filters['color'], 'blue', 'red', 'green') AS f_color"));
$query->where("is_active = 1");
$query->andWhere("f_color = 1");
$announces = $query->all();
There is jSON field filters in my Sphinx index. For example:
[filters] => {"brand":"Toyota","model":"Prius","color":"red","price":"12000"... etc]
It works OK. But now I need to make a pagination... and there is a problem when I try to count records before $query->all()
$count = $query->count(); // Return error "no such filter attribute 'f_color'"
Generated query was:
SELECT COUNT(*) FROM announcements WHERE ( is_active = 1 ) AND ( f_color = 1 )
count() by default replaces the select part with * and this is where your alias is defined hence the error.
There are different ways to achieve it like:
use ActiveDataProvider like described here,
use META information like described here
Since you want to make a pagination I would go with the first example.

Sum expression with IIF clause

I want to sum field called: Fields!Horas.Value depending of Fields!sNombrePlanta.Value
So I do:
=Sum(IIF(Fields!sNombrePlanta.Value = "AUCHI", Fields!Horas.Value, 0))
It work perfectly, but now I want to add another Fields!sNombrePlanta.Value and sum this two, so I try:
=Sum(IIF((Fields!sNombrePlanta.Value = "AUCHI" And
Fields!sNombrePlanta.Value = "AUQRO"), Fields!Horas.Value, 0))
and it just return blank value. What am I doing wrong there? Regards
You are asking one field to have two values:
=Sum(IIF((Fields!sNombrePlanta.Value = "AUCHI" And Fields!sNombrePlanta.Value = "AUQRO"), Fields!Horas.Value, 0))
sNombrePlanta can't have both the values "AUCHI" AND "AUQRO" at the same time. Do you mean to use OR where the field is either of those values?

Select value of one field into another on the same MySQL table in from the same row?

If I were to translate my MySQL table into PHP it might look something like this:
$table = array();
$table[0] = array ('id'=>0, 'f1'=>0, 'f2'=>1);
$table[1] = array ('id'=>1, 'f1'=>0, 'f2'=>2);
// etc...
In that case, I would want to do something like this:
foreach($table as $row) {
$row['f1'] = $row['f2'];
}
Is it possible to do this with a single MySQL statement utilizing select and update?
I was imagining something like this:
update myTable set f1=(select f2 from myTable where id=id);
Except I don't think that would work... I'm not sure how to say where id in the second statement is equal to the id in the first statement. Or how to apply it to all rows in the table.
How could I do this?
Actually, to update the f1 values to be the same as the f2 ones you'd use:
UPDATE mytable
SET f1 = f2
If you don't specify a WHERE clause, the query will apply to all rows.
Just
UPDATE myTable SET f1 = f2
the expression on the right of the = is in terms of existing columns of the same row (one often does, e.g, SET col = col + 1... it doesn't have to be OTHER columns, though it can). The lack of a WHERE clause means the update will happen on all rows, as you seem to want.