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;
Related
I'm a little bit stuck.
I have an SQL column that contains weather codes (like Rain, Snow, etc.)
It is comma separated so the column would have a value of something like
rain,snow,haze
Now, I want to select the rows that contain values from an array.
I have an SQL code that is something like this:
SELECT * FROM locations WHERE currentWeather IN ('rain', 'snow', 'cloudy') ORDER BY name ASC
The problem is that this obviously works when currentWeather column only contains one item.
Is there a way to do it so that if the column value contains any of the items from the given array, it selects it?
Also, would it select it twice if two items match?
Best wishes
Use unnest in a subselect.
Select distinct A.myArray from (select unnest(column) as myArray from table) A where A.myArray in (your words to filter for)
Notice that using arrays in sql isn't very ideal and does not follows normalization rules. Your tables should ideally not contain arrays but rather just several rows each one containing the specific value you Want. It prevents issues such as this one.
To avoid the selection of repeated values, use the Distinct keyword right after you write select.
Rsference:
https://www.w3resource.com/PostgreSQL/postgresql_unnest-function.php
WHERE FIND_IN_SET(currentWeather, "rain,snow,cloudy")
Picks apart the string at commas (only) to see if currentWeather is any one of those 3 'words'.
See also FIELD(...)
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.
I have some category list. And have some posts those have multiple category.
such as ..
php is a category,, The first post has php, JavaScript, WordPress category and the second post has php, MySQL category in one column.
Now when I click on the php category, that time all the posts which have php category will be retrieved.
please any idea how can I retrieve all posts based on one category if those has multiple category in one column.
What you need is FIND_IN_SET() MySQL function.
SELECT * FROM TableName WHERE FIND_IN_SET('PHP', category_column);
FIND_IN_SET() returns position of the PHP in category_column, so used in WHERE clause it is a true for everything except 0, which is returned when substring was not found.
As a disclaimer I'd like to mention, that it seems, you have bad design of your database. You shouldn't put multiple values in one field. You may get in trouble with developing further features in you app later.
You could use FIND_IN_SET, something like..
SELECT * FROM table_name
WHERE FIND_IN_SET('php',`your_colum_name_containing_values`);
I am new to sonr and I have to write a conditional query. I just wanted to know can I write a query with multiple conditions.
I have four column and the query condition is something like following:-
Columns: name, address, city, state
Now if someone is searching by keyword "Romania" then the result should be based on following conditions:-
Search in name columns if keyword match with the name column then return the data and do not go for any further column.
If name column doesn't have that keyword then it will go to the address column and similar to the first step if result found then return the data otherwise it will go to next column.
It is a priority search in which the priory of columns to be used in search is predefined. If sonr provide any such functionality I want to use that instead of writing any fuzzy logic.
Thanks
This would be to search all the columns you're talking about. Use qf to tell Solr which fields you want to query (when using the edismax query type):
qf=name address city
You can also give weights, so that a hit in the name column will be shown higher than a hit in the address column, which in turn will be shown higher than a hit in the city column:
qf=name^20 address^10 city
You might have to adjust the weights to get the search result you want.
You could use fq to search it like these:
fq=name:romania
or in the q
+name:"romania" or +address:"romania" or +city:"romania" or +state:"romania"
using OR is like in programming that if the first query is false, then move to the next one and to the next.
This is kind of a weird question so my title is just as weird.
This is a voting app so I have a table called ballots that has a two important fields: username and ballot. The field ballot is a VARCHAR but it basically stores a list of 25 ids (numbers from 1-200) as CSVs. For example it might be:
22,12,1,3,4,5,6,7,...
And another one might have
3,4,12,1,4,5,...
And so on.
So given an id (let's say 12) I want to find which row (or username) has that id in the leading spot. So in our example above it would be the first user because he has 12 in the second position whereas the second user has it in the third position. It's possible that multiple people may have 12 in the leading position (say if user #3 and #4 have it in spot #1) and it's possible that no one may have ranked 12.
I also need to do the reverse (who has it in the worst spot) but I figure if I can figure out one problem the rest is easy.
I would like to do this using a minimal number of queries and statements but for the life of me I cannot see how.
The simplest solution I thought of is to traverse all of the users in the table and keep track of who has an id in the leading spot. This will work fine for me now but the number of users can potentially increase exponentially.
The other idea I had was to do a query like this:
select `username` from `ballots` where `ballot` like '12,%'
and if that returns results I'm done because position 1 is the leading spot. But if that returned 0 results I'd do:
select `username` from `ballots` where `ballot` like '*,12,%'
where * is a wildcard character that will match one number and one number only (unlike the %). But I don't know if this can actually be done.
Anyway does anyone have any suggestions on how best to do this?
Thanks
I'm not sure I understood correctly what you want to do - to get a list of users who have a given number in the 'ballot' field ordered by its position in that field?
If so, you should be able to use MySQL FIND_IN_SET() function:
SELECT username, FIND_IN_SET(12, ballot) as position
FROM ballots
WHERE FIND_IN_SET(12, ballot) > 0
ORDER BY position
This will return all rows that have your number (e.g. 12) somewhere in ballot sorted by position you can apply LIMIT to reduce the number of rows returned.