My table has a column that is in array type, and each in each row contain an array of length of 100.
I want to get the top 10 value out of each array and I only know how to get top 1 value using ARRAY_MAX(column)
How can we get top N value instead?
Getting top N out of an array is different from getting top n value out of a regular column and rows where you can do
SELECT column \\ FROM table \\ ORDER BY column DESC \\ LIMIT 10
May I suggest you correcting the tag info if the question is not MySQL based ? But if we are indeed using MySQL as the MySQL tag implies or if you are simply curious about how MySQL copes with array, I believe it does not have an array type for columns and not really needs one. This can be accomplished using a primary table with an array_id column and a second table which references the primary table's array_id and stores individual values through each row. The second table may look like this:
array_id array_value
1 443
1 80
1 8088
2 3306
2 1521
3 22
Thus the TOP N values can be easily retrieved. If by TOP N values you mean the top N highest values , we can use a similar query as the one you provided: SELECT array_value from array_table where array_id=n ORDER BY array_value DESC LIMIT 10;
Or if you mean the top n values in terms of the sequence in the array, we can use:
SELECT array_value from array_table where array_id=n LIMIT 10;
In the latter case, no ORDER BY clause is required in this case as we would like to retrieve values based on the sequence of rows being inserted. Usually it's the default sequence for a column's values to show up without index if ORDER BY clause is omitted. However, if you are really worried about the sequence without ORDER BY, you are free to add a timestamp column or PK auto_increment id column and use it in the ORDER BY clause to guarantee the sequence.
Related
Is it possible to grab the last n inserts in a relational table without using a Date field ?
For example in the table Author:
Author(authid, f_name, l_name)
Also, authid is not a natural number (eg. 1,2,3,4..) but a string (example: JohnM32015)
I am using MySQL.
If the authid is auto-increment then you can do
select * from author
order by authid desc
To get only a limited number of records use top n in SQL-Server or limit n in MySQL for instance.
A table doesn't have a guaranteed sort order. A query does, if it explicitly defines one. If, for example, your records have an incrementing authid value then the last N inserts would be the highest N values for that column. So you'd order by that column descending and take the top N:
SELECT * FROM Author ORDER BY authid DESC LIMIT 10
However you define "the last N", you specify that definition in your query in a descending sort order and take the top N records from that result.
If, as you say, you want the "most recent" 10 records, you would write your query just like the other answers say, but order by the field(s) that defines your case for "most recent". Like this:
SELECT * FROM MyTable ORDER BY <your date field(s)> DESC LIMIT 10;
Disclaimer: If you don't have data in the table to define whatever should be the "most recent" records (like a "DateInserted" field, or an auto-incrementing field), then you don't really have an easy way of doing this with SQL.
I want to write a query to select a subset of a table, only starting from a given id.
I know about limit x, y, but x here is the number of the raw to start from. But in my case I want to start from a specific Id, no matter what its location inside the table.
What I mean is that the query below selects from row number 5, but I want it to select 10 records from row with id, say 213odin2d211d21:
SELECT * FROM my_table Limit 5, 10
I can't find a way to do this. Any help will be appreciated.
Note that, the Id here is a mix of strings and integers. So I can't do
SELECT * FROM <table> WHERE id > (id)
What you want to do is not possible. By default, records in the database are not ordered. Without ORDER BY you can't expect the server to return your queries in any particular order. Since you are saying, that you store some kind of digit/char identifier as your id, for which less then and greater then are not defined, it is not clear which records "follow" your specific record.
You will either have to:
Define another column to sort your records on, or
Define a behaviour for comparing your ids (What is "less then"? What is "greater then"?)
That being said, you can of course define that you want to sort your id just like sorting strings! In this case, you can use STRCMP() to compare two strings. Your query would look like this:
SELECT * FROM <table> WHERE STRCMP(id,?) = 1 ORDER BY id LIMIT 10
This will select the first 10 records, with id "greater than" ?.
I am trying to select a user at random from a very simple table for the purposes of generating sample data.
The table has just two columns, the integer primary key users_id which has all the values from 1 to 46 inclusive, and uname which is a varchar(60).
The query
select relusers.uname from relusers where relusers.users_id=floor(rand()*46+1);
is returning multiple rows. Perhaps I've been staring at this for too long but I fail to see how the above query could ever return more than one row. floor() returns a single integer which is being compared to the primary key column. Including users_id in the selection shows multiple different IDs being selected. Zero rows as a result I can understand, but multiple? Any ideas?
Your code is returning multiple rows because rand() is evaluated on each row. So, you have the change of multiple matches. And a chance of no matches at all.
You can use your idea, but try it this way:
select relusers.uname
from relusers cross join
(selext #rand := rand()) const
where relusers.users_id = floor(#rand*46+1);
This generates just one random value and hence just one row. But, with only 46 rows, the order by method should perform well enough:
select relusers.uname
from relusers
order by rand()
limit 1;
select * from relusers order by rand() limit 1
I have a table which has only 3 columns. When I type the following query
select * from MyTable order by 5 and 2;
I get every thing in the table( The result is equal to that of select * from MyTable; ). What I originally expected is that I would get some kind of error. But I didn't get it, why?
order by 5 and 2 is interpreted as order by (5 and 2) which is a constant expression, hence no real ordering is done and data is simply shown in the order it was inserted.
What is happenning here is that 5 and 2 is seen as an expression which is evaluated to 1. However, it shouldn't give a result sorted by first column.
Actually, I think you only get sorted data because you inserted it in sorted sequence. Take a look at this SQLFiddle:
http://sqlfiddle.com/#!2/3e04e/1
The data is not sorted by any of the columns, it is being sorted by a value 1.
5 and 2 are column indexes and they mean 5th column and 2nd column.
select * from MyTable order by 5,2;
It means start with index 5 and bring 2record i.e 5,6,7
I am trying to create a search engine from a mysql datadase which displays information in order based on the value of one of the columns in the row, so if row x has a 'quantity' column
with an integer value of 10 any row y has a 'quantity'value of 20, row x should be echoed first so the greatest value is on top and the least on the bottom (no preference for equal values). or alliteratively find a way that the mysql table automatically orders the data this way. is there some kind of function that I could use. thanks
Use the ORDER BY clause Like this:
SELECT * FROM table ORDER BY quantity DESC
To return rows from a query in a specific order, use the ORDER BY clause. To get them returned from highest to lowest values, specify the DESC (descending) keyword. For example:
SELECT f.quantity
FROM foo f
ORDER BY f.quantity DESC
You are correct, there is no guarantee as to how the rows will be physically ordered when they are stored. Your ONLY guarantee is the ORDER BY clause.