This question already has answers here:
MySQL: Select Random Entry, but Weight Towards Certain Entries
(11 answers)
Closed 8 years ago.
I'm working on a simple advertisement script, I have a database with all the ads and a weight.
The ad is a picture and the weight is a number from 1 to 20 depending on it's importance (1 shows up rarely and 20 shows up a lot.
But if I'm getting this information from my database how do I sort and choose an ad to display? So can I put a weight on a variabele in a creative way? It's possible to make and array for each weight point and then select a random ad from that array but is there a better alternative?
#Dagon is correct. Use the example from that other question. Again Dagon found it here: MySQL: Select Random Entry, but Weight Towards Certain Entries The comments are really helpful. You basically multiple that row by its weight. To add to that question and answers I would add the rows into an array and then randomly pick one (one array value) to display. When something has more weight just place it in that array times its weight.
Related
This question already has answers here:
mysqli query only returning first row
(5 answers)
Closed 7 years ago.
I'm learning PHP and MySQL and I have a question.
<?php
require('../connect_db.php');
$q='SELECT price FROM towels WHERE color = "Red"';
$r=mysqli_query($dbc, $q);
while($row=mysqli_fetch_array($r, MYSQLI_NUM)) {$var=$row[0];}
In my case, I have towels with different prices that have color="Red". But it seems that the output in $var is only one of those prices.
It doesn't even seem to be always the largest or smallest price, or the price belonging to the first PrimaryKey in my table, or anything else that would seem intuitive.
so what is going on here?
Thanks.
For ordering you need to add following to the end:
ORDER BY price ASC
You are iterating throught your results in while. So every iteration $var gets the price of the next element. Depending on what you want to do with the prices you can list all of them in a seperate array. But as you didnt discribe what you want to do I can't exactly help you with that. If you edit that i can continue further on helping
This question already has answers here:
MySQL pagination without double-querying?
(9 answers)
Closed 7 years ago.
As I know a pagination structure requires minimum two sql query.
First, find total row
Second, limit your query.
Is there a way to decrease query to one. Can we use first sql query to manupulate the pagination? On first query we already fetch all necessary data. Is first query's array can handle this issue?
You can do a simple previous/next pagination with a single query.
For this i your result limit is say 25, just query for 26 and only display the 25. If you get back less than 26 results, you know you don't have any more.
However if you are wanting to accurately display links for page 1,2,3,etc.. you have to do both a query for the total number of records in the table, and a query for just the data you want to display.
This question already has answers here:
How can I return 10 of the most recent results in sql?
(4 answers)
Closed 9 years ago.
How can i ask a question to a mysql-database (via PDO) to return the 3 latest added rows? The id column is key and auto incremented. That means that highest id means latest added. Don't take into account the fact that rows can have been deleted and such.
Could i somehow use * and LIMIT 3 and start from the bottom some way - or something?
Should be fairly easy but i am kind of stuck.
Order by ID desc limit 3
gives you the three rows with the highest ID. Those are not necessarily the latest three added rows. But they are the latest three added rows still existing in the table.
This question already has answers here:
SQL query, select nearest places by a given coordinates [duplicate]
(4 answers)
Closed 9 years ago.
I want to make a table of with columns in a MySQL database:
Index
Latitude
Longitude
Places like place of Countries, Cities, People, Building etc.
With huge number of rows, in order of hundred thousands until million of rows.
If I want to get nearest places of a selected row in the table, how can I do that in the fastest way?
It is no problem if more information, indexing, or presorting are necessary.
======
Edit 1:
I have read the answer and the answer is using a formula, for example from the best answer:
(((acos(sin((".$latitude."*pi()/180)) * sin((geo_latitude*pi()/180))+cos((".latitude."*pi()/180)) * cos((geo_latitude*pi()/180)) * cos(((".$longitude."- geo_longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344)
If I have 1 million rows, that means, there are 1 million of expensive calculation. I thing it will be very slow.
Are the optimization, for example using filtering in the beginning:
1. If the input is City A in location 10.000, 20.000, then filter cities that located at 9.000 to 11.00.
2. Calculate with the formula above.
How to optimize the speed of that algorithm?
====
Edit 2:
Sorry, I've only read the best answer.
I found what I've looked for in the other answer: http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
You can use a quadkey. A quadkey is a spatial index like a quadtree. It sort the points into a grid and then you can search the grid around the center point. It's not easy to understand but you can download my php class hilbert-curve # phpclasses.org. Or you can use the native MySQL spatial extension and the point datatype. However my implementation uses a quadkey and a hilbert-curve and can be better. It depends much on the data. The problem with the harvesine formula is that it is very slow. But you can use both algorithms together to achieve better results.
This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I am using an IN clause, whereby, it needs to search the id IN these set of IDs that are not in order, meaning, it came from a different criteria., how to solve this ?
SELECT DISTINCT ID FROM "projeck"."mytable" "t" WHERE staffID IN (75953,196262,196387,133585,195639,196702,195790,195820,192903,145383,179603,175896,176554,43545,154843,183798,195767,195715,etc..etc.. etc..)
and i am getting this oracle error
General error: 1795 OCIStmtExecute: ORA-01795: maximum number of expressions in a list is 1000
My first choice would be to reference the function that generates these values directly.
If the values were being used for multiple queries and were expensive to calculate then I'd think about loading them into a global temporary table and joining to it.