Using a field atrribute in mysql parameter function cast - mysql

I want to use a variable (or an attribute of the table) as CAST parameter (AS), I cant find the proper syntax online.
The proof of concept looks like this:
SELECT
prd_template_field_type,
#type := prd_template_field_type,
Min(CAST(prd_template_field_rule_value AS #type)) AS Min,
Max(CAST(prd_template_field_rule_value AS #type)) AS Max
FROM tbl_prd_template_field
Please dont downvote, I really try to give my best to formulate this as clear as possible + long research has been done.
Thanks!

Related

Using query as a query parameter

I am using poorly documented API, so I suppose that there is a common practice since they didn't explained the way it should be used. API endpoint supports following query parameters: query (string) and pageNumber (integer) - the part that confuses me is query inside of query.
For example, I want to check all orders with property that has some value: https://api.logsta.com/orders?query=orderIdentifier=106300 but it doesn't work. For me, the natural behaviour would be https://api.logsta.com/orders?orderIdentifier=106300 but this is impossible since they require this query query parameter.
Based on your experience, what should I pass into this query to make it work? Is it a SQL expression or there is a standardized approach?
you can use url encode
?query=orderIdentifier%3D106300
I found the solution, and it works, so I hope that this scenario could be useful to someone.
https://api.logsta.com/orders?query=orderIdentifier:300000169928409
instead of using = again or URL encoding, they are accepting :

Select a sum in Knex.js without using .raw

I am trying to rewrite some MySQL queries in Knex.js, and I feel like I'm running into .raw at every turn, which feels counter to the reason I want to use Knex in the first place.
Is it possible to write the following query without using .raw?
SELECT
product,
SUM(revenue)
FROM orders
Using raw, it works to write:
knex()
.select(
'product',
knex.raw('SUM(revenue)')
)
.from('orders')
but the idea of using Knex was to avoid using MySQL query strings, so I'm hoping there's another way. Or does everyone just use .raw everywhere, and I'm misunderstanding something? Very possible, I'm new to this.
You can use the sum method.
sum — .sum(column|columns|raw) Retrieve the sum of the values of a
given column or array of columns (note that some drivers do not
support multiple columns). Also accepts raw expressions.
knex('users').sum('products')
Outputs:
select sum("products") from "users"
Probably be something like this:
knex()
.select('product')
.sum('revenue')
.from('orders')
You should adjust to your specific case. You might need to use something like groupBy('product') to get total revenue per product.
You should really go over knex's documentation, it's pretty good and straight forward and you definitely should not be using raw all the time.
You can even specify the returning sum column name like this:
knex(tableName)
.select('product')
.sum({ total: 'revenue' })
.groupBy('product');

Can i use question mark in MySQL this way

i'm using node.js to build a web. I connect to my database (Mysql) and
get the data then return to the client side.
I know that i can use the literal template to write my sql expression like below
promise(`SELECT table.name FROM table WHERE name = ?`,[parameter])
and i'm would like to know can i write my sql expression like this?
promise(`SELECT table.? FROM table`,[parameter])
I know the result is different, i just want to know it is right or not.
thank you and have a nice day.
No, but you could use string interpolation like:
promise(`SELECT table.name FROM table WHERE name = ${parameter}`)

Time calculation for time value entered in SQL

I need help on calculating my time difference. I search on the forum but not what I need. Here is the code I am using:-
(convert(varchar(10),([RT_Phase_Time])-(convert(time,'00:30:00'))))
So I [RT_Phase_Time] is in this format 'hh:mm:ss'. I am trying to get the difference between ([RT_Phase_Time] - '00:30:00'). Please help!
In MySQL you can easily do that with TIMEDIFF().
TIMEDIFF('22:00:00','00:30:00');
-- Returns '21:30:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Try something like this, check the BOL for datediff to convert to other values:
declare #rt_phase_time time
set #rt_phase_time = '12:30:30'
select datediff(ss,cast('00:30:00' as time),#rt_phase_time)

Creating an OR statement using existing conditions hash

I am working on a problem where I need to add an OR clause to a set of existing conditions. The current conditions are built in a hash in a method and at the end, they are used in the where clause. Here is a simplified example:
...
conds.merge!({:users => {:archived => false}})
Model.where(conds)
I am trying to add an OR clause to the current set of conditions so it would be something like '(conditions) OR new_condition'. I'd like to add the OR statement without converting each addition to the conds hash into a string. That would be my last option. I was hoping someone has done something like this before (without using Arel). I seem to recall in Rails 2 there was a way to parse a conditions hash using a method from the model (something like Model.some_method(conds) would produce the where clause string. Maybe that would be a good option to just add the OR clause on to that string. Any ideas are appreciated. Thank you for your help!
I found a way to do what I needed. Instead of changing all of the conditions that I am building, I am parsing the conditions to SQL using sanitize_sql_for_conditions. This is a private method in ActiveRecord, so I had to put a method on the model to allow me to access it. Here is my model method:
def self.convert_conditions_hash_to_sql(conditions)
self.sanitize_sql_for_conditions(conditions)
end
So, once I convert my conditions to text, I can add my OR clause (along with the appropriate parentheses) to the end of the original conditions. So, it would go something like this:
Model.where('(?) OR (model.type = ? AND model.id IN(?))', Model.convert_conditions_hash_to_sql(conds), model_type, model_id_array)