Query to select one random Row without any repeats - mysql

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;

Related

Update multiple columns in first row with data from other table without join

I'm moving a few boolean columns from the 1st row of a generic settings table into the 1st row of a website_settings table across a few MYSQL databases. I've created the new columns in my new table with a default false value.
I have a working query to copy data from the old table:
UPDATE website_settings
SET
dark_mode_enabled = (SELECT dark_mode_enabled FROM settings ORDER BY id LIMIT 1),
header_enabled = (SELECT header_enabled FROM settings ORDER BY id LIMIT 1),
footer_enabled = (SELECT footer_enabled FROM settings ORDER BY id LIMIT 1)
LIMIT 1;
However for my own knowledge, I'm curious if there is a more cleaner way to write this, perhaps without the repetitive select queries to the same table?
You could use a join:
UPDATE website_settings ws CROSS JOIN
(SELECT s.*
FROM settings s
ORDER id DESC
LIMIT 1
) s
SET ws.dark_mode_enabled = s.dark_mode_enabled,
ws.header_enabled = s.header_enabled,
ws.footer_enabled = s.footer_enabled
LIMIT 1;
Here's a solution that does not use JOIN:
INSERT INTO website_settings (id, dark_mode_enabled, header_enabled, footer_enabled)
SELECT id, dark_mode_enabled, header_enabled, footer_enabled
FROM settings ORDER BY id LIMIT 1
ON DUPLICATE KEY UPDATE
dark_mode_enabled = VALUES(dark_mode_enabled),
header_enabled = VALUES(header_enabled),
footer_enabled = VALUES(footer_enabled);

Using Limit random on 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)

Multi Subquery count function unable to take the stress of multiple selects

This has me stumped.
MySQL
UPDATE sets SET sets.current_count = (SELECT COUNT(leads_auto.set_id) AS current_count FROM leads_auto WHERE leads_auto.set_id = (SELECT sets.set_id AS setID FROM sets WHERE sets.on_off = 0)) WHERE sets.on_off = 0
Seems right doesn't it? Update the record current_count with the total number of rows in leads_auto which have the set_id value of the set_id of the sets table where the value of its on_off column is 0.
yet I get this error
#1093 - You can't specify target table 'sets' for update in FROM clause
I looked around and someone mentioned that it has to do with the operation being cyclic?
Create a temporary table for the result of SET
UPDATE sets
SET sets.current_count =
(
SELECT COUNT(leads_auto.set_id) AS current_count
FROM leads_auto
WHERE leads_auto.set_id =
(
SELECT set_id
FROM
(
SELECT sets.set_id AS setID
FROM sets
WHERE sets.on_off = 0
) c
)
)
WHERE sets.on_off = 0

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;

MySQL: display page containing record

Assume the following records:
ID Value
=========
1. No
2. No
3. No
4. No
5. No
6. No
7. Yes
8. No
9. No
10. No
Each "page" of my records-display contains 5 records (so page 1 has records 1-5, page 2 has records 6-10, and so on ...). I want to display the page that contains the record with the value of Yes. Keep in mind that I don't really know where this Yes is in the records.
How do I query this?
How about something like this:
/* Work out the page */
SELECT #MatchID = ID
FROM tbl
WHERE Value = 'Yes'
ORDER BY ID ASC
LIMIT 1;
SELECT #Page = CEIL(COUNT(*) / 5)
FROM tbl
WHERE ID <= #MatchID;
/* Select the items on that page */
SET #Offset = (#Page - 1) * 5;
SELECT *
FROM tbl
ORDER BY ID ASC
LIMIT #Offset, 5;
Note: The above doesn't cater for #MatchID not being found.
I'm unsure whether MySQL limits have to be constants, hence if they do you would have to calculate the offset in PHP, or whatever programming language you're using to connect to MySQL. Alternatively, maybe this would work, instead of the last SELECT statement in the above example:
SET #selectSQL = CONCAT('SELECT * FROM tbl ORDER BY ID ASC LIMIT ', #Offset, ', 5');
PREPARE stmt FROM #selectSQL;
EXECUTE stmt;
SELECT ID FROM tbl WHERE Value = "Yes" Order by ID ASC LIMIT 1;
divide the ID through the "records-per-page" (in your case: 5).
Then you get the "page" where the first yes-record exists.
Assuming your ID is Primary Key (auto_increment) and without gaps.
SELECT Value, ID / 5 AS Page WHERE Value = "Yes"
EDIT
SELECT Value, ID DIV 5 + 1 AS Page WHERE Value = "Yes"
Alright, I think I got this now.
select all records on the same page as the record with Value = 'YES'
SET #c:=0;
SELECT tbl.* FROM tbl WHERE CEIL((#c:=#c+1) / 5) = (SELECT CEIL(COUNT(temp_table.id) / 5) FROM tbl temp_table WHERE temp_table.id <= (SELECT temp2.id FROM tbl temp2 WHERE temp2.value = 'YES')) ORDER BY tbl.id ASC;