mysql result from WHERE and the rest - mysql

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.

Related

How to treat missing id 's value as 0 and order by it?

When I use in keyword in sql, there may be some id is missing , but I want treat them like they exist and other columns are null or 0.
For example, suppose I have a table with two columns and some rows:
[id,value1]
1      1
2      4
3      3
5      5
I may write sql like this:
select * from table where id in (1,4,5) order by value1 limit 0,2 ;
When this sql is executed, the return result is [(1,1),(5,5)].
But what I want is [(4,0),(1,1)], because I want to treat the missing id 4 like it exists in the table.
So the question is : Is there some elegant way to achieve it using sql instead of select all rows and sort them in memory.
Use a left join:
select *
from (select 1 as id union all
select 4 union all
select 5
) i left join
table t
using (id)
order by t.value1
limit 0, 2 ;
Note that you are ordering by a value in the existing table, so this depends on the fact that NULL is ordered before other values.

MySQL Query to replace string with value

I have requirement like as below.
Need a MYSQL query to replace value with maching the below condition.
i have a table containg the Product ID
Product_ID
1
2
3
4
5
15
25
I want to replace the 5 with value of 1.111. My requiremnet is this that it should only replace the 5 value not the 15 value.
example 5 should be 1.111 but it sould not replace the 15 value.
You can use IF() or CASE to select a different value when the value meets a condition.
SELECT IF(product_id = '5', '1.111', product_id)
FROM yourTable
or
SELECT CASE product_id
WHEN '5' THEN '1.111'
ELSE product_id
END
FROM yourTable
CASE generalizes more easily to other values that you want to replace, since you can have multiple WHEN clauses.

Mysql search text in one column

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`

How can I return the next row of WHERE clause?

I am using mysql DB server ..
I have the following table that consists of only one column with the following data (where 0's separate sorted integers)
Number
-------
0
1
2
3
0
4
5
0
6
7
8
0
9
10
0
11
I want to get the first value that comes after each 0 , so e.g. output would be
Output
------
1
4
6
9
11
MySQL doesn't guarantee a return order unless you specify a ORDER BY clause. If you get the values back in the order you insert them then that's just coincidence. As it stands there's no way to do what you want to do reliably. You need to add something to order the data by. An ID field set to autoincrement will probably do.
SELECT id1 FROM table WHERE id IN
(SELECT t1.id+1 FROM table t1
LEFT JOIN table t2 ON t1.id1=t2.id
WHERE t2.id1 IS NULL);
SQL Fiddle
Assuming no gaps in the increment field,but since it will be created..
First of all you should add Auto_increment key to your table to guarantee order of records.
Alter table T ADD id INT PRIMARY KEY AUTO_INCREMENT;
And here is the query:
select * from T as T1
Where (SELECT Number From T where Id<T1.Id ORDER BY ID DESC LIMIT 1) = 0
order by ID
SQL Fiddle demo

Random row from big query result

I need to get 1-2 rows from query result retrived with SQL select on indexed columns without getting the whole record set.
For example I will retrieve 10 000 records using query
SELECT * FROM table WHERE field 1>1 AND field1 < 10
but I need only 1 random row from this query regarding to highload of my database.
I can use
SELECT * FROM table WHERE field 1>1 AND field1 < 10 LIMIT 100, 1
But I don't know records numebr to use correct offset range
How can I achieve this goal?
You could use ORDER BY RAND()
SELECT * FROM table WHERE field1 > 1 AND field1 < 10 ORDER BY RAND() LIMIT 1
This will return 1 random row with field1 in between 1 and 10
How about restricting the records you select in the first place?
SELECT * FROM table WHERE field1 IN (CONVERT(RAND()*10,SIGNED),CONVERT(RAND()*10,SIGNED)) LIMIT 2