SQL Query using Random - mysql

Hi could anyone help me with something?
I need a mysql query that can return 4 values from a column in some table, also select all fiels from each row.
something like:
SELECT * FROM dadoslivros WHERE RAND() =1 limit 100;
But i only want the random form row ID.
Thanks.

It seems like you just want to use ORDER BY RAND():
select *
from dadoslivros
order by rand()
limit 100
See SQL Fiddle with Demo

Related

SQL multiple column ordering randomly

I want to order highlight by rand() without modifying the syntax. How to do ?
I've tried with ORDER BY servers.highlight rand() but still not working.
ORDER BY `servers`.`highlight` DESC, `servers`.`votes` DESC,`servers`.`online_players` DESC,`servers`.`status` DESC
The only way to achieve this is to do the random ordering of servers.highlight in the original JOIN. Attempting to ORDER BY RAND() at the end will simply give random ordering to the entire output. Something like this:
SELECT *
FROM (SELECT * FROM servers.highlight ORDER BY RAND()) h
JOIN servers.votes ON ...
I've created a small demo on dbfiddle to demonstrate.

SQL SELECT TOP statement is not working properly

I am trying to view the first 2 records of a table name Customers which have two columns name Name(varchar) and Salary(text) in MySQL server 6.0
The command which I am using is:
SELECT TOP 2 * FROM customers;
But it's not working.
I recommend you to write statement like this using LIMIT
SELECT * FROM customers [WHERE conditions] [ORDER BY expression [ASC|DESC]] LIMIT 2 ;
Instead of
SELECT TOP 2 * FROM customers;
It's very simple. Just try this
SELECT * FROM customers LIMIT 2;
You can check the manual also.
Use this in case of MySQL:
select * from customers limit 2;
To get any position value in the whole tale I suggest you to use the following query as TOP [position] doesn't work in all versions of MySQL.
select min(column_name) from (select column_name from table_name order by column_name desc limit position_you_sort) any_name_for_table;

SQL First() Function

I am using phpMyAdmin to write some SQL code that I thought was simple but proving to be a headache. I'm using this tutorial to help me out. My goal is to get the first and last columns id's from a result set. When I do this query I get 5 rows starting at 15 and going through 11.
SELECT id
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
However, when I try this query I get an error #1064: "You have an error in your SQL syntax."
SELECT FIRST(id)
FROM boardPost
WHERE recipientId = 1
ORDER BY id DESC
LIMIT 0,5
Something like this maybe?
SELECT min(id), max(id)
from (
select id
from boardPost
where recipientId = 1
order by id desc
limit 0,5
) t
I think that is what you want?
Select id from boardPost order by id asc limit 1
and
Select id from boardPost order by id desc limit 1
If you just want the first and the last id of a result set, you could consider this:
SELECT MIN(id) firstId, MAX(id) lastId FROM someTable WHERE aField = 1;
Note that it'll only work if you do use and ORDER BY an AUTO_INCREMENT field, else you might get unexpected results.
It'll only work with the full set. If you need the first and last id of a limited one, you're probably better of using 2 queries with ASC and DESC order and LIMIT 1.
MySQL does not support the FIRST() function. You will need to use the workaround they specified in that tutorial (using ORDER BY and LIMIT)
In some situations (like mine, that first brought me here), there are some rarely-used MySQL "windowing functions" such as FIRST_VALUE and LAST_VALUE that may provide the functionality you're seeking.
(Here's more info on Window Function Concepts and Syntax, and the Wikipedia description of the term.)

Return only one tuple of SQL query result

I want to do a query like
select * from chr2;
but only have MySQL return the first tuple (or an arbitrary) tuple instead of all of them.
How do I do it?
Use the LIMIT clause:
SELECT * FROM chr2 LIMIT 1;
If you want an arbitrary row returned, you have to sort your rows by an random col like this (MySQL docu):
SELECT * FROM chr2
ORDER BY RAND()
LIMIT 1;
On large tables, however, you might run into performance problems with this, as there a random value has to be created for each row and the table has to be sorted according to this column.
Try this ::
select * from chr2 limit 1

Omit the first 5 rows?

I want to SELECT all rows except for the first 5 rows in a table.
How do I do that?
Why can't I just type
$query = "SELECT *
FROM ages
OFFSET 5
ORDER BY id ASC";
SELECT * FROM tbl LIMIT 5,18446744073709551615;
from http://dev.mysql.com/doc/refman/5.0/en/select.html
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
In Oracle:
select name, price
from items
where rownum > 5
Here's a solution using variables - just add your order by clause and you should be set.
set #n=-1
select * from TABLE where (#n:=#n+1) >= 5;
I just typed:
$query = "SELECT *
FROM ages
LIMIT 100
OFFSET 10";
Why couldn't anybody give me such an easy answer? :)