Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Saving Random Query


I want to select about 10 rows randomly from my table every day. I know how to randomly select rows, but is it possible to save that select so that I get the same 10 randomly selected rows again?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Saving Query Results
Is there any way to save the results of a query without the quote marks?
When i try to import the text file that I saved the query to, I have to go thru and delete all of the quote marks.

Auto Saving Results From Query Browser Into Excel Format
Is there a way I can save the output(s) of select statements (in query browser) to excel files directly , so I can run this as stored procedure or script? (I have quite a few queries and wanted to automate the process so anyone can do it.)

Random Query
I have been tasked with getting daily quotes out of a sql database, there needs to be 3 that are randomly selected. I have not found any documentation in regards to doing this.

How would I qurey a database and pull 3 quotes of the day out randomly. This should even occur when a user refreshes the page, is there a sql function that would do this? or do I need to do it via php, perl, etc...

Random Query
$querystring = $querystring." ORDER BY status, dateposted desc";

How can i make my query that its sorts first by status and then random for date posted.

INSERT Query With Random Date
I have about 2,000 entries to add to my database and I want each one to have a random date of between, for instance, 2007-01-01 and 2007-03-28 [today].

Any ideas on what's the best way of doing this?

I did try and come up with a script that grabs a line, updates it and then moves on to the next line, but I keep getting "This page will repeat forever" errors coming up.

Random City During Query Circulation
data in myTable1

(id) name
(1) Danaka
(2) Jane
(3) Jean

data in myTable2

(id) city
(1) Tokyo
(2) New York
(2) Chicago
(2) L.A.
(3) Paris
(3) Lyon
I have two tables like the above.

The following codes produce the following results.

code1

select myTable1.id, name, city
from myTable1
left join myTable2 on myTable1.id=myTable2.id
where myTable1.id=2

result1

(2) Jane New York
(2) Jane Chicago
(2) Jane L.A.
code2

select myTable1.id, name, city
from myTable1
left join myTable2 on myTable1.id=myTable2.id
where myTable1.id=2
ORDER BY RAND() LIMIT 0,1

result2

(2) Jane L.A. //random city
I like to produce my target result below.


target result

(2) Danaka Tokyo //random city
(2) Jane Chicago //random city
(2) Jean Lyon // random city

Random Query, How To Keep Sort Order Locked?
PHP Version 5.1.6 - mySQL 5.0.22

Let's pretend we have 100.000 rows in a table (all containing images with details), and I want to display 100 images at a single frontend page, but randomly retrieved and keeping the sort order once initiated to a user session.

With the normal RAND function, you have a chance to stumble on the same images when you navigate to the next page with again 100 items on. But even worse, going back to page 1, will return totally different results, and will freak out the viewer

How to get a (fake) random listing on a fairly large database, where every requested row is unique, and the sort order is being locked (until you press a randomize button ie, or start a new session) while keeping speed in mind (so trying to avoid reading out all rows up front).

Random Rows, Non-random Ordering
Normally to select random rows from a table I'd do something like:

SELECT field1, field2, RAND() AS random
FROM mytable
ORDER BY random
LIMIT 5
Unfortunately, this time I need 5 random rows sorted on a different field:

SELECT field1, field2, RAND() AS random
FROM mytable
ORDER BY field2
LIMIT 5
Is there a way that I can still sort by field2 and yet still select 5 rows at random? Could the former query be run as a subquery and then a second ORDER BY clause run on those results? Worst-case I can re-sort the results in my host language, but a pure SQL solution would be best I think.

Bonus points if the answer can always put a particular row first in the results.

Saving PDF In Database
I am trying to save PDF files in a database. I have made an upload page with PHP and interface the database with SQLyog to do the design and other things as well. When I upload my file using my upload page it uploads everything just fine (images, excel files, and pdf files). However when I try to download them via another php page everything works but the PDF files are corrupted somehow.

The error I get in Adobe PDF: "There was an error opening this document. The file cannot be found"

I then try and get the files thru SQLyog and the same problem. Right now I have them being saved in a longblob. Is this the right data type? Why would everything work but PDF files?

Saving A Database
I just started using MySql and I am lost. I understand how to put all the info in the command box but I need to know how to save it so I don't have to start all over. Also how do I open it again?

Saving .avi Into Database
how to save or store .avi into database

Saving Backward Slash
I don't know exactly "" is called forward slash or backward slash.
But I guess it is called backward slash

I like to save it into my database with the following code.

update myTable
set topic=��'
where ID=1
But
��' is actually saved, instead of ��.'

I don't know why the backward slash is not saved in myTable.

How can I save it?

Saving Mysql Db To Access?
is it possible? with which tool?

Saving And Using Email Addresses?
I am developing an application that will store contact information. One of the values will be an email address. I would like the user to be able to do a search and if the email address is returned be able to have it returned as a link that they can click on and create the email message. In otherwords, I would like them to be able to send the email directly from the results and not have to copy and paste it into their email client. MS Access allows you to use a Hyperlink data property. Is there anything similar in MySQL? Can anyone advise me on the best way to do this?

Saving Text In Russian
I have a table with a field that is to save text in Russian. The text is
then retrieved by a PHP page. I run a query passing text in windows-1251
encoding, but when I retrieve it from the db, it shows up all wrong.
Does anyone know what the problem is?

Saving Database To File
How would you go abouts saving all data in the mysql to a file and download it? I know how to retreive the data and write it (dont know how to make a download button) but how do i record the table data, such as make them inserts like you would in phpmyadmin.

Then with a click of the button in mysqlsave.php, it retrieves the information, saves it to mysqlDATA.doc and then downloads it to your hard drive?

Saving An SQL File For Uploading
I have saved a create table script, but have noticed that it appears to be only as a TXT file rather then SQL file typ. How do i change this extension so that i can get SQL to find it?

Daylight Saving And CONVERT_TZ
So here's kind of a weird deal... I'm adding a timezone feature to an existing system and since all of the datetimes are stored in MySQL tables I'm planning on storing all datetime values as Grenwich times (+0:00) and then using MySQL's CONVERT_TZ() function, I will convert that time to the timezone the user has set his profile to use. Example, CONVERT_TZ(create_ts, '+0:00', '-5:00') to display a datetime in the Eastern time zone.

So the weird deal is, what is going to happen between the date the Grenwich timezone comes off of daylight saving time (October 29th 2am I think) and the time U.S. timezones are still on daylight saving time (thanks to silly congress)? Will the CONVERT_TZ() function make the adjustment?

I did some testing using the datetime 2007-10-28 23:00:00 and it does not seem to give an extra hour.

SELECT CONVERT_TZ('2007-10-28 23:00:00, '-5:00', '+0:00')
//returns 2007-10-29 04:00:00 (does not give the extra DST hour)

Is this going to be a problem?

Best Practice When Saving Different Datatypes
I am working on a system where I need to save data from a questionnaire that will be of different datatypes either an integer, a boolean/bit or a text value (string to text size).

Currently I am working on a model where I create two tables to hold the different datatypes and a column in the question's table to indicate whether the answer should be stored as an integer or a string.

Is there some better way to do this? As far as I know I shouldn't create one column to hold all datatypes since the overhead of using a text column to store small integer values (eg. 1-5) would be enormous(?)

Mostly the answers will be in the form of small integers like 1-5 or 0-100.

Saving Foreign Characters
For some reason my tables dont seem to store foreign characters. Am I using the wrong data type?

Saving Content On Database
i need to know this because i am making a movie website. I could make every movie on a single html page but then i would create thousands of them which isnt really handy. So i am looking for a way to save content into my database + a image for every movie. Is this possible? adding content and images on databases and then show them in html pages (with php linking)?

if so, could someone plz explain how i can do this and what the php linking code is? In the html files i need a code like:
get/fetch #31
and 31 is for example The Aviator. It imports the image and content and its done. Now i dont need to edit the html file, except adding that code.

Saving Rows Into A File
I did search the forum but I found nothing.
Is there a SQL function that selects specific rows from a table and saves them into a file as SQL statements, which can then be executed later?
I basically want to save sme rows as a set of INSERT statements incase they get deleted and need to be restored.
This is intended as a tool for the client-tier so I would like the file to be created on their machine - not on the host server.

Saving Images In A Table
is it possible to save a whole image-file in a mySQL table? I mean I know that I could e.g. save a file by ftp to the server and save the url to that file in a mySQL table, but I wonder wether it´s possible to save the file itself in a mySQL table and if so, how?

Comparing Two Tables And Saving The Differences In Another
I am trying to take a new table of info and compare it to an existing table. (both are lists of names and ssn's.) The missing ones are the ones needed to put in to the existing. I also have to add some dates and payment info not in the existing.
So.

I cannot find the comparison method that outputs the nameLast and nameFirst that is in one table but not the other.

Saving Databases In User Directories
I would like store user databases in their user directory (/home/username/Databases/dbname/)
and I know I can use symlinks, but I find that very cludgy. Is there something in mysql that
is similar to apache's setup where I can specify a home path to the public_html location or
in this example, a database location?

I am using version 4.0.13 on Red Hat Linux 9.

Saving Storage Space On Blank Entries In DB
If I have a table, of say.... people, with their names, ages, addresses
etc., and some lines in the table have the ages BLANK, how do I make
sure that the blank fields are not taking up disk space? Does OPTIMIZE
TABLE do this, or is another command?

Connector/ODBC Not Saving Port Setting
Using MySQL Connector/ODBC 3.51, I set the port to a non-default value
(3309), under 'Connection Options' to connect to a remote machine running 2
versions of MySQL. 'Test' is successful but when I close the Connector/ODBC
window on XP the port setting is not saved so the DSN doesn't work.

Saving Geocode Information, What Data Type?
What data type is best to use to save Latitude and Longitude information? If I use float, I only have precision 4 (xx.xxxx). I can't use text because I want to do calculations with it. What data type are you all using?

Saving A Div With An Image Makes Mysql Table Garbled
I wrote a script in php, the script takes all the divs in a page, pouts then in an array then save to a mysql blog.

Works like a charm if there are no images in the divs, but if I insert an image with insertImage() then save, the table gets all garbled, outputing an error when read.

Problem Saving Javascript Content In MySQL Database
I have been using MySQL for eight years now, but the problem I am having has me feeling like a newbie.

I am the owner and operator of a busy message board website. I recently moved over to a new dual Opteron 2218 system. When I built out the box, I install MySQL version 5.0.32. Since this changeover, I am unable to add any content containing HTML Javascript script calls to any database field. When I try to do so, the request simply times out. I have tried to update DB info using the template editing system in vBulletin and tried to add data via PHPMyAdmin, with the same results. Not only can I not add such content, I cannot modify or edit any data field that already contains any HTML markup using script tags to call a Javascript process. I am baffled.

If anyone has any idea why this could be happening, I would really appreciate hearing it. My site is operational now, but I cannot make any modifications to it or add any Javacript banners until I resolve this issue.

Guests Saving Information To A Databse And Linking DB Data To Accounts
When someone logs into my site, they can generate an image. I would want them to, after logging in, be able to click a link (or button) that would save the image(url) in a DB. Then, by visiting another page, they could visit all the images they have saved(urls) which would recall them based on their username.

Sql Random Pwd
i read somewhere that it is not safe to use a mysql randomly generated password . I have a table with many records and want to reset everyone's password to a random alpha-numeric password.
What is the best way to do this one ? something using a php random pwd and then update the table ? or there is something better than that ?

Random Row
I am trying to get one random row at a time. Right now I am using this SQL:

Code:

SELECT * FROM `trtrack` ORDER BY RAND( ) LIMIT 0 , 1

But it's really slow, some queries taking up to .5 to 1 second. Is there any other way? I use PHP.

Add A Random Value
in php, this would simply be rand(1,5)
in mysql, I tried

update `table` set column=(column+rand(1,5));

but that was not a correct guess.
how can I add a random value of 1 digit bt x and y?

Random Row Select?
if I have a table with 200 rows in it. I want to select 1 random row. Does anyone know how to do that in a select statement?

Random Keyword
I have a question about what query I should use to achieve the following...

I have a table called 'keywords' that has the following fields

id (auto_increment)
pagename
keyword

There can be multiple keywords for one pagename, but each keyword has it's own row. For example, this could be in the table:

id pagename keyword
11 test-news test
10 test-news news
9 test-news article
What I want to achieve is to select 1 random keyword for each unique pagename. So there can only be 1 keyword selected for a pagename, but it should happen for all unique pagename entries. What query would I use to achieve this?

Random Retrieving
data in myTable

(id) name
(1) Tom
(4) Jane
(8) John
I have data in myTable like the above.

Can I retrieve one of names in name column randomly?

Random Images
is there sombody who can helpme witht he following, i am ''pretty new'' to
php and am looking for a script to show images randomly at a 10/ 15 seconds
interval.

Random Function?
I would like to know if there is an existing random function I can use to generate a random number so I can concatenate it to a string?

Select At Random?
I have a db that I'm using as a mailing list. In many (but not all) cases, there are several contacts per company. I rarely want to send my mail to more than 1 person at any one company, and I never want to send to more than 3.

I don't have any criteria that I can use to select which contact to select, so I want to do it essentially at random. So I want to contruct a SELECT query that says, "when there are >1 contact at company X, pick 1 (or 2) of them at random. When there is only 1 contact at a company, include that contact/company in the result as well."

I'm sure there's an easy way to do this, but having searched online and read a lot of the 1500-page manual, I haven't figured it out.

Inserting Random #
I have a db name image records. It has 3 columns recno_no, file_name and active_fl.

is there a way to insert 100 random #'s into the database table and its columns for testing?

Random Records
I'm trying to get a random record for use with my website. I've tried figuring out some premade code and it's led me nowhere..

Here's basically the rundown...

I have a database setup by my site's shopping cart system, and in it are 2 tables i need to read from, "specials" and "featured". I want to pull 4 random entries from "specials" and 1 from "featured" to have posted on my site's main page outside of the store.

I know how to connect to and select the database, I even know how to send queries, but the whole selecting a random entry is what's got me at a brick wall.

I'm not used to PHP and MySQL, i'm just now learning after 4 years of using Perl(and never using a database system, but creating my own database-like files).

Random Select
When I do a select with a limit I get always the same records. Usually this
is the desidered effect. But now I want to do a random select. So every time I do a select I should get a different set of results. What is the best way to achieve this effect?

Order By Random
Is there anyway to order records randomly? instead of order by id desc to order it randomly??

ORDER BY Random
Is it possible to pull results from a database using a SELECT... ORDER BY and pull them in a random order?

Random Pick
When an artist isn't picked, there is a random artist picked. Now I have it setup so it counts the rows, and picks a random number from 1-nr of rows. This number is then used as the id and gotten a mysql query from this id.

$query = mysql_query("SELECT * FROM `artists`");
$total = mysql_num_rows($query);
$random=rand(1,$total);
$right = mysql_query("SELECT * FROM `artists` WHERE `id`=$random");

Now, I want something a bit different. I also have ABC....Z, by the first letterof Artist Name. If the first letter is picked, but no artist, I want the random artist to be start with this letter. I wanted to use something like above but the id won't be in order. it could be like 1, 20, 32, etc etc.

Random Data
I keep getting random data put in to by database??? Any idea how to stop this?

Random Row Order
What do I need to put into a SQL statement for it to retrieve 100 Random Rows (containing certain criteria in the WHERE clause). Something like this (theoretically) is what i'm looking for:

SELECT *
FROM table
WHERE field="yes"
ORDER RANDOM
LIMIT 0, 25

Any Ideas?

Random Ports
mysql 5.0 was working fine in Debian "etch".

Then I installed awstats, which changed something somewhere and now the server starts on a random port. However, no clients can FIND it! How do I resolve this issue?


Copyright © 2005-08 www.BigResource.com, All rights reserved