Using Limit random on mysql - mysql

I just used this query
SELECT * FROM questions
ORDER BY RAND()
LIMIT 20;
On mysql database I have a column called display (along with the columns of questions) where the values are equal to 1.So now as you can see in the query I have limited to 20.I want to change the value of the all the 20 to display=0.I know this is the query
update test3
set display=0
where id=11;
But this just sets the display of one row.How do I do it for the 20.
Thanks in advance.

you can do this
update test3
set display=0
where id in (select id from questions order by rand() limit 20);
Suppose you are using php.
$result = msyql_query("select id from questions order by rand() limit 20");
$ids = array();
while($row = msyql_fetch_assoc($result)){
$ids[] = $row['id'];
}
For your condition, first perform the first query and save the ids from the first query.
Suppose you are using php and you have saved the ids from the first query in $ids;
you can run the second query like this;
$idstring = implode(',',$ids);
$query = 'update test3 set display=0 where id in ('.$idstring.' )';
$result = mysql_query($query);

Use the WHERE clause to search a subquery
UPDATE test3
SET display = 0
WHERE id IN (
SELECT id FROM questions
ORDER BY RAND()
LIMIT 20)
If you want to perform something between the SELECT and the UPDATE try this:
CREATE TABLE #Temp (
division TINYINT
)
INSERT INTO #Temp
SELECT id FROM questions
ORDER BY RAND()
LIMIT 20
--SELECT * FROM #Temp
UPDATE test3
SET display = 0
WHERE id IN (
SELECT division FROM #Temp)

Related

Get the records from an ID to the last record

I'm trying to get all the records from this ID to last record inserted.
example: 40 records and I want to display data from id(33) to id(40).
Something like this.
Select * from records between 33 and last_id;
My current SQL.
sql = "SELECT id, B5, B6, datetime FROM child_records ORDER BY id DESC LIMIT 10";
Use following
This query return all record where id greater than 32
Select * from records where id > 32;
You make the first $sql query to always get the last id in table
$last_id = always the last id in the table
Then in $row['id'] it is the last id you got from SQL query. You store it in variable then use it in main query
// query to get last id
$sql = "SELECT * FROM `child_records` order by `id` DESC LIMIT 1";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$last_id = $row['id']; // now you got the last id in table even if you inserted new columns
// main query
$sql = "SELECT `id`, `B5`, `B6`, `datetime`
FROM `child_records`
WHERE `id` BETWEEN 33 AND {$last_id} ";
Then you know how to complete the query right ? just like the query to get last id
hope that answer your question

Query to select one random Row without any repeats

I have a table with 3 columns, as follows:
Columns:
ID
Channel_Location
Used
I would like to retrieve a random entry from the table and update Used column to 1. However, when I run my code - shown below - it returns 0 rows and doesn't return any idea. I was wondering why is this case?
The code
UPDATE channels
SET Used = 1
WHERE ID IN (
SELECT ID
FROM (select ID
FROM channels
WHERE Used != 0
ORDER BY RAND()
LIMIT 1) x);
One way to address this issue, is to do as follows:
SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
UPDATE channels SET Used = 1 WHERE ID = #uid;
SELECT * FROM channels WHERE ID = #uid;

Read a dynamic MySQL with PHP

I have a dynamic MySQL-Table with 20 columns and 1 Row where id = 1. The new data will be added to the table as the sencond row where id = 2, And so an. I need to read the newest data row (always the last id). How should I write the query to read the last row?
How should I change this:
$sql = "SELECT id, AbW_L, PuVor_L ,Durchfluss ,... FROM table order by id desc ";
Order by id descending, and limit the results to one row:
SELECT * from table order by id desc limit 1;
You might want to take a look at: http://www.techonthenet.com/sql/order_by.php
It will give you more detailed information on what to do, however based on that one line of code, just change it to:
$sql = "SELECT * FROM `table_name` ORDER BY `id` DESC";
Should work no problem. If you want to add limits, you can do:
$sql = "SELECT * FROM `table_name` ORDER BY `id` DESC LIMIT 10";
I selected 10 as the limit in the example, you can pick whatever number you would like to use.

update columns based on query with select

I have a data base that I imported into a table called ip2city that contains 3 columns (startipint,endipint,country)
that is linking a given IP range (represented in integer) to a specific country
I have another table called Test2 that containing columns source and dest (INT representing IP) and empty columns countryS, countryD
I want to fill the empty columns based on the the data from the table ip2city
I am new to mysql
I tried something that looks like :
UPDATE Test2
SET CountryS = (SELECT Country FROM `ip2city` WHERE startipint <= Test2.`Source`
ORDER BY startipint DESC LIMIT 1);
where select... is suppose to return the value I want to insert to the table
but it dosnt seem to work
when I just use
SELECT * FROM `ip2city` WHERE startipint <= 3232235521 ORDER BY startipint DESC LIMIT 1
with specific ip, I get a good result so how can i use it on an entire table.. ?
Try this:
UPDATE Test2
JOIN ip2city s ON Test2.Source between s.startipint and s.endipint
JOIN ip2city d ON Test2.Dest between d.startipint and d.endipint
SET CountryS = s.country, CountryD = d.country
Note that this is a mysql-only solution, because this query uses mysql's multiple-table update syntax.
$result = mysql_query("SELECT * FROM ip2city WHERE startipint <= 3232235521 ORDER BY startipint DESC LIMIT 1");
while ($row = mysql_fetch_array($result) {
echo $row['columnname'];
Now you can use this in your UPDATE query like this:
UPDATE Test2
SET CountryS = $row['columnname']

MySQL - Update and Select in one query

I'm updating a table like so...
Update table Set count = count + 1 Where id = xx Limit 1
How to get the value of count without querying the table again? Can it be done in one query?
Thank you!
No.
Update does not return a result set.
However you can get the count without having to query the table
UPDATE `table` SET count = #count:= count + 1 WHERE id = 'xx' LIMIT 1;
SELECT #count as LastUpdateCount;