I want to get all the columns from the products table and apply sql case on pick_8 and pick_12 columns only but to do this i have to write all the column names in select. Is there any way in laravel where i can apply the simple get() method to retrieve all the columns with applying case on only 2, without having to write all the column names manually.
$products = Product::where('is_archive',0)->select('humanId','name','cost','retailPrice','memberPrice','points',
DB::raw('(CASE WHEN pick_8 = 1 THEN "True" ELSE "False" END) AS pick_8'),
DB::raw('(CASE WHEN pick_12 = 1 THEN "True" ELSE "False" END) AS pick_12'))->get()->toArray();
To retrieve all the columns you can use DB::raw method also.
$products = Product::where('is_archive',0)->select(
DB::raw('products.*'),
DB::raw('(CASE WHEN pick_8 = 1 THEN "True" ELSE "False" END) AS pick_8'),
DB::raw('(CASE WHEN pick_12 = 1 THEN "True" ELSE "False" END) AS pick_12'))->get()->toArray();
CASE statement will overwrite original values of pick_8 and pick_12 columns.
P.S. By the way. I guess you trying to use wrong tools to reach your goal. Take a look at eloquent-mutators
Related
I am having three parameters $page, $date, $search. i need a single select sql query where i have to check all the three variables one by one whether any of these variables is having some value or not or if all of the three variables is having some value. According to which it will match from the database and give the result. Please help me out.
You can make where clause as follows in your Select query.(Below is MS SQL format, you can modify CASE statement equivalent with MY SQL)
SELECT * FROM TABLE (if more than one table, you can use join)
WHERE TABLE.search_COLUMN = case when #search <> '' then #search else TABLE.search_COLUMN end
AND
TABLE.page_value_COLUMN = case when #page_value <> '' then #page_value else TABLE.page_value_COLUMN end
AND
TABLE.date_value_COLUMN = case when #date_value <> '' then #date_value else TABLE.date_value_COLUMN end
I need an SQL query that selects from Column A if Column C contains the string 'ebook'. Otherwise, select from Column B.
So something like:
IF (Table.ColumnC = "ebook") SELECT Table.ColumnA AS Publisher
ELSE SELECT Table.ColumnB AS Publisher
Select
case when columnC = 'e-book'
then columnA
else columnB
end as Publisher
from myTable;
You need a case statement
I reformatted to help show how this works.
Basically Case is a scalar (row per row) IF statement
There can be multiple conditions WHEN, similar to else if. The function goes from condition to condition, if no conditions are passed, the ELSE value is used.
There are simple and search forms of CASE Documentation
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
select case columnC when 'ebook' then columnA else columnB end as publisher
from my_table;
In MySQL you can write it like this:
SELECT IF(Table.ColumnC = 'ebook', Table.ColumnA, Table.ColumnB) AS Publisher FROM Table...
If you have more than one possibility, then go for CASE..WHEN..THEN..ELSE..END.
I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.
I'm about to build some sort of function or query where I can check if a certain record already exists in the database. The following rules apply:
The table has 6 columns
My yet-to-build-query has access to a complete row-object (all 6 values)
This query should find each row with at least 4 out of 6 corresponding values from the object I passed
Using MySQL
Is it even possible to build a query like this? My goal is to have a function which can return true if it's likely that a row like the passed object is already existing in the database.
Is my only option to make a query with multiple where-statements (where I try for each combination 4 different values)?
pseudo:
function getSimilarRow(Row_Object $row)
{
//select *
//from table_x
//where 4 out of 6 properties from object $row apply
}
You could use a case statement in the where clause for each property you are trying to match. If it meets the criteria then give the case statement a value of 1; if it doesn't then give it 0. The sum of the cases should then be >= 4.
I'm not that familiar with MySQL but the following will work (I knocked up a quick SQL Fiddle to show it working):
select * from SomeTable where
(case when propertyOne = 'value1' then 1 else 0 end) +
(case when propertyTwo = 'value2' then 1 else 0 end) +
(case when propertyThree = 'value3' then 1 else 0 end) +
(case when propertyFour = 'value4' then 1 else 0 end) +
(case when propertyFive = 'value5' then 1 else 0 end) +
(case when propertySix = 'value6' then 1 else 0 end) >= 4
Obviously you could change your logic in each clause if you'd prefer them to be likes or anything. You could even apply a weighting to each column by using something other than just 1 if you needed to get really creative.
I want to filter data based on an array, similar to "in" keyword in a query:
SELECT count(*) as number_of_records,
count(CASE WHEN result = 'SUCCESS' THEN 1 END) as successful_builds_no,
min(CASE WHEN result = 'FAILURE' THEN 0 ELSE 1 END) as is_success,
min(duration) as best_duration,
max(build_date) as build_date
FROM mdl_selenium_results
WHERE build_date in ('2014-03-13', '2014-03-12')
GROUP BY build_date
How to achieve that using Moodle DB api??
If you are creating an SQL query to use within Moodle, you could just use the IN keyword, exactly as you have done in the example.
Alternatively, to make it a bit more cross-DB compatible, and to make your queries run a bit faster when there is only one item in the list, you can write:
list($dsql, $dparam) = $DB->get_in_or_equal(array('2014-03-13', '2014-03-12'), SQL_PARAMS_NAMED);
$sql = "SELECT count(*) as number_of_records,
count(CASE WHEN result = 'SUCCESS' THEN 1 END) as successful_builds_no,
min(CASE WHEN result = 'FAILURE' THEN 0 ELSE 1 END) as is_success,
min(duration) as best_duration,
max(build_date) as build_date
FROM {selenium_results}
WHERE build_date $dsql
GROUP BY build_date";
$result = $DB->get_records_sql($sql, $dparams);
OR, if you just want to get all the matching records from the given table (and then worry about doing the calculations in PHP), you could write:
$result = $DB->get_records_list('selenium_results', 'build_date', array('2014-03-13', '2014-03-12'));
(I've not run any of the code above, so apologies for any typos)