Mysql search text in one column - mysql

For Example our table;
İd--------Price---------Level
1 ------100-300 ------ 1,2
2 ---------200----------1
3 ------100-280--------1,3
We want search a price value is 110. 110 is between 100-300 and 100-280 so id 1 and id 2 must listed. Can we write this query with my-sql?.
Additional , we want search price and level value. Price 110 and level 2 searching. Can we write this query with my-sql?.
Thank You

Remember that database tables should be created with the idea that it will satisfy your query needs. It doesn't make sense to have a table with a price "100-300" which represents a String (or in mysql a VARCHAR) and you want to treat this as a number. So what to do?
1) The first thing i would do is re write my table schema having a minPrice and maxPrice fields, so this way you could have this:
İd----minPrice---maxPrice------level
1 ------100---------300 ------ 1,2
2 ------200---------200----------1
3 ------100---------280--------1,3
2) Then your query would be like:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice
3) In case you also want to look for a level value. you would do:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice AND myLevelValue IN (x.level)

Use two columns for the price ...
ID FROM TO LEVEL
1 100 300 1,2
2 200 200 1
3 100 280 1,3
Then SQL:
SELECT `level` FROM `table`
WHERE X >= `from` AND X <= `to`

Related

Query which Find string and increment the count

I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..

how to move up row to find the last or first occurrence of the same number in mysql

I have a table name current_record in mysql database.
id num
1 0
2 1
3 1
4 1
5 0
my question is : how find the 1st id of num = 1 from last means down to up or 3 times up
the output should return like this
id = 2 num = 1
please write a sql query.
You can use MIN() combined with GROUP BY to achieve this. Something like this:
SELECT MIN(id) AS id, num FROM current_record GROUP BY num
Optionally you can add a WHERE clause if you specifically want just one value instead of one for each num:
WHERE num == 1

mysql result from WHERE and the rest

assume that I have a table like this
table name = myTable
id
----
1
2
3
4
5
6
7
is there any way to fetch some rows with this query
SELECT * FROM `myTable` WHERE id IN (1,5,7) OR ... ;
and in fill the ... with any thing that give me the result for WHERE clause first,
and the rest of the rows after those rows???
somethig like this:
1,5,7,2,3,4,6
You don't do this with a where, you do this with an order by. Like this:
order by (id in (1, 5, 7)) desc
MySQL interprets boolean values as integers, with 0 being false and 1 being true. The desc will put the true values before the false ones.

MySql Select Id <= Maximum value in the table

When using the <= in a MySql statement, is it possible to make MySql select the maximum value in a table without supplying a value to <= in the sql statement?
Eg:
id
----
1
2
3
4
5
6
Eg:
// start from the last record when no value is supplied
select id from table where id <= * ORDER BY id DESC LIMIT 5
Result
6
5
4
3
2
// start from the 5th record when a value is supplied
select id from table where id <= 5 ORDER BY id DESC LIMIT 5
Result
5
4
3
2
1
My problem is, this statement is in a php function, so I cannot change it dynamically. Is it somehow possible to do what I'm trying, or perhaps another way to get around this?
You can use NULL instead of using *
SET #var_search_value = NULL -- OR 5
SELECT id
FROM table
WHERE id <= #var_search_value OR #var_search_value IS NULL
ORDER BY id DESC LIMIT 5
If you want to get every record which is less than or equal to the maximum value in a particular column, then logically you want to get every record:
select id from table ORDER BY id DESC LIMIT 5
No WHERE clause is required.

sort particular rows using mysql order case

I have a table with incremented id from 1,2,3...and so on. What i want is just to sort the data in descending order on the basis of field 'id' except first two rows. I tried using below query:-
SELECT * FROM categories ORDER BY CASE WHEN id<3 THEN 0 ELSE id END DESC
It give me the result like
id name
5 Meal
4 Apparel
3 Electronics
1 Sports
2 Lifestyle
But output should come like
id name
1 Sports
2 Lifestyle
5 Meal
4 Apparel
3 Electronics
Is there any way to achieve this by using such query?
Try this:
SELECT *
FROM categories
ORDER BY CASE WHEN id < 3 THEN 10000 - id ELSE id END DESC;
-- ----------------------------^
-- use a very large number
Edit:
A better solution which does not require hard coding 10000:
SELECT *
FROM categories
ORDER BY CASE WHEN id < 3 THEN id ELSE 3 END, id DESC
-- ---------------------^ ^
-- ------------------------------------|
-- these numbers must be same
Change it to:
CASE WHEN id<3 THEN id ELSE ~id END
And instead of case, use IF:
IF(id<3, id, ~id)
That's because you're ordering by descending ids, so the order is 5-4-3-0-0. You can try:
SELECT * FROM categories ORDER BY CASE WHEN id<3 THEN 9999 ELSE id END DESC
But that is not a perfect solution