I have a table as follows:
I want to select the first row having the unit value greater or equal to 2000 and less than 3000. I can do it as follows:
select * from spot_shipment_fees where min_unit>=2000 AND max_unit<3000
But the problem is I have only one variable holding a single value like 2000. How can I generate this SQL in this situation?
i.e: $unit_value = 2000.
select * from spot_shipment_fees where min_unit>=$unit_value AND max_unit<$unit_value
I know this is wrong but can't find the tricks to resolve it. Any idea?
I think you just want:
select ssf.*
from spot_shipment_fees ssf
where ? between min_unit and max_unit;
? is a parameter placeholder for the value you want to pass in.
Related
Is there a possible way to get the value from a custom parameter inside a column.
As you can see in the picture below. I have a column name Parameters, and has a value of custom parameters.
1.) Is there any way that I can only get the price and its corresponding value inside that column name?
2.) Is there any possible way to decipher the format?
3.) Can you give me a idea how to parse it accordingly?
Click this to see the picture
Im just curious guys. Thanks in advance for those who will helping me out.
Assuming the string pattern always starts like 'price<=>' (i.e. 9 characters in start). You can use below query to check the price and compare it.
The inner query uses substring to find the price value from matching rows. Outer query can be used to perform comparison as you want.
SELECT
*
FROM
(SELECT
SUBSTRING(parameters, 9) AS price
FROM
your_table
WHERE
parameters REGEXP 'price<=>[0-9]') t
WHERE
price > 1000;
I got table promos, with field store_id_list (VARCHAR). what i want to achieve here is i want this promo can be available into multiple store in 1 record, instead using multiple record, the store_id_list value is the list of store_id separated by comma (ex: 1,4,5,7,)
Now, i want to get record of table promo, where i have single store_id value, ex: store_id=5 how can i do that in MySQL? can i do that in single query? if using LIKE then more likely i can get 15 or 45 instead of 5.
My advice is to normalize your tables, and have a db-record per store/promo. But it can be done like this:
Make sure you have commas at the beginning and end of the column value, like this:
store_id_list : ,1,4,5,7,
And then query like this:
... where store_id_list like '%,5,%'
I think you are looking for FIND_IN_SET function. For example:
SELECT * FROM tbl WHERE FIND_IN_SET('5', store_id) > 0;
I have a table methodology in mysql where a column contain values like 1,2,3,4,5,6 or 3,4,6 or 25,4,7,8 in multiple row.
Now in my scenario i have a company which contain id 6 and i want to Match this value with methodology table value. But i don't have any idea how i can do it.
I am trying that first it get all values from methodology table one by one and after that match value of company.
can anyone please help me??
You can use FIND_IN_SET(your_id, columnname) function.
Query will be like
SELECT * FROM
methodology
where FIND_IN_SET(your_id, columnname)
For more details about function please refer : http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
Use LIKE from mysql queries.
An example :
SELECT row FROM my_table WHERE my_column LIKE %6%
You need to use find_in_set function
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
To elaborate on #matthiasbe's incomplete answer, if you want to use LIKE, you should do as follows :
SELECT * FROM methodology WHERE companies LIKE '%,6,%' OR companies LIKE '6,%' OR companies LIKE '%,6'
This will match all rows where companies either has ,6, in its value or begins with 6, or ends with ,6.
This is far from being a perfect solution (both in clarity of the code and execution time), I'm just listing the options, hope it helps.
How to select last record (that is having MAX(id)) from the table?
Next statement works OK, but selects the first record:
$statistics = SystemStatisticsHistory::findOne(1);
To get the model with max id you can apply reverse order and limit to one.
SystemStatisticsHistory::find()->orderBy(['id' => SORT_DESC])->one();
Another option is to use subselect with max like so:
SystemStatisticsHistory::find()
->where(['id' => SystemStatisticsHistory::find()->max('id')])
->one();
There are some nuances using last option, check this question.
You can check the documentation for max() here.
I personally prefer using first variation.
To get the first record, just change the order direction to SORT_ASC in first query and max() to min() in second query.
P.S. Hardcoded id is a bad practice.
Optimised one
SystemStatisticsHistory::find()->select(['id'=>'MAX(`id`)'])->one()->id;
OR if you want increase by number
SystemStatisticsHistory::find()->select(['id'=>'( MAX(`id`)+ 1) '])->one()->id;
Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10