Randomize x through y rows in a MYSQL database - php

For example, how can I randomize rows ten through 20?
I only know how to randomize all rows using RANDR().
I am using PHP + MYSQLi.
Can someone explain me using MYSQL RANDR() ? I know it's not efficient.
Thanks,
Tom.

I have used a technique with SQL Server that might port to MYSQL. I assign a new GUID to each row, then sort on that.

Related

Find closest match by number in MySQLI

I'm working on a PHP / MySQLI application, where the user needs to input a number, and then it should get the 5 closest records to that given number.
Can this be done by a simple SQL string, or do I need to get all the numbers into an array, and then match by that..?
Thanks!
This is possible through following query:
SELECT * FROM [table]
ORDER BY ABS([column] - [userinput])
LIMIT 5
However, if you could provide more information we would also be able to provide you with a better solution. This query is not very scalable and will after a couple of thousand rows start to get slow.
How are you going to use this query? Are we talking thousands of records? What kind of numbers is it? Is there some pattern? All such questions would allow for a more precise solution that could possible scale better with your system.

Do a calculation in SQL query or PHP?

this is my first post, so I hope I follow all the conventions :D
I have a MySql database with x and y coordinates and a php web service that receives other x and y coords. I want to calculate the square distance: (x2-x1)^2 + (y2-y1)^2 and sort from closest to farthest
SQL query (in php) would be:
SELECT ((x_coor-$x)*(x_coor-$x)+(y_coor-$y)*(y_coor-$y)) AS SquareDis FROM $table ORDER BY SquareDis
What is more gentle to performance, doing this in a SQL query or in the php program?
Thanks for all answers in advance!
If you want to sort by the results, then do the calculation in SQL.
Otherwise, you are just using the database as a "file store" and not taking advantage of the functionality that it offers. In addition, by doing the ordering in the database, you can limit the number of rows being returned -- another optimization.

Sybase IQ cache database result

I'm using a query that calculates some values on a table with about 11 millions rows. And I need to display the results in real time (on my site), but this calculations need about 1min to execute. The table content changes each 30 mins, so I don't have to recalc the results at each time user reloads the page. How can I cache the results of calculations? Via php (I use odbc) or using some sql statement, some sybase IQ option. Thanks.
I also asked this question at https://dba.stackexchange.com/. So sorry for duplicating, can't figure out where is the better place.
So I found the solution. Not optimized, but helpful for me. I insert my calculations into temp table, and add there a column with current date. On a script start I'm checking if table is older then 30mins, and if so, I drop it and crwate again.

Mysql query to show results from between two columns

Hi i have a mysql database, in which i have two columns Year_from & Year_two.
what i am trying to do is find a way where i can show the dates that are missing as buttons, for example if year from is 2006 and year to is 2008, i of course want to show 2006, 2007 and 2008. is this possible, as there isn't the value of 2007 in the database.
I haven't worked on any code yet as i am not sure if this is possible, or how i would achieve it.
Any ideas would be appreciated.
Thanks
Use range($year_from, $year_to) to generate a list of all years. Compare that array with the one you got from the database using array_diff() and bold the missing ones.
This needs to be done in code.
Basically, you'll read a bunch of records from the database, iterate through an array of years you'd like to show and check (every iteration) if there is a matching record present in the result set.

Basic PHP/MySQL math example

I have a PHP form that grabs user-entered data and then posts it to a MySQL database. I'd like to know, how I can take the mathematical difference between two fields and post it to a third field in the database?
For example, I'd like to subtract "travel_costs" from "show_1_price" and write the difference to the "total_cost" variable. What's the best way to do this? Thanks so much.
You can lately process a select query: SELECT show_1_price - travel_costs AS pricediff FROM my_table; and then grab value in php and again do an insert query...
Should be simple to do on the PHP side of things how about
query=sprintf("INSERT INTO table VALUES(%d, %d, %d)", travel_costs,
show_1_price, show_1_price - travel_cost);
Generally though it is bad form to store a value in a database that can be calculated from other values. The reason being that you may never ever access this value again yet you are using storage for it. CPU cycles are much more abundant today so calculate the value when need. This is not a golden rule though - there are times when it could be more efficient to store the calculated value - although this is not usually the case.

Categories