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.
Related
i have table like this
Table
I just want to display the data based on the most data appearing in the second field and I do not want to display a little data. So the data appears only the most and count the total value in the third field. And I've tried but no results.
How to query the database to display the most data only
You can do something like this,
SELECT `col_name_of_frequent_value`,
COUNT(`col_name_of_frequent_value`) AS `frequent_value`
FROM `table_name`
GROUP BY `col_name_of_frequent_value`
ORDER BY `frequent_value` DESC // This will sort the result by putting max count at top.
LIMIT 1; // This will only show the TOP-Most value in sorted result.
Please change the names as per your table structure.
SELECT
kd_masalah, total_bobot
FROM
(SELECT -- SubQuery
SUM(bobot) AS total_bobot, -- Gives the sum of values in bobot column relating to a particular group (grouped column)
kd_masalah
FROM
your_table_name
GROUP BY kd_masalah -- Grouping of same values in the column
)AS s having MAX(total_bobot); -- return the values having max sum in total_bobot
I have scenario on MySQL MariaDB 10.20.1 where a resulting set of rows can not be sorted by a specific column.
Here is oversimplified example of this query
SELECT *
FROM (
SELECT *
FROM Invoice
WHERE /SOME_COMPLEX_CONDITION IS TRUE/
UNION ALL
SELECT *
FROM Invoice
WHERE /ANOTHER_COMPLEX_CONDITION IS TRUE/
) D12 ORDER BY D12.Amount ASC;
Resulting set gets sorted in lexicographical order, rather then in ascending or descending order. The type of Invoice column is DECIMAL(X,Y), and there is not type conversion in the code.
I want directions where should I start my research on this?
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 have an EAV table in SQL which as usual has several records for each ID. Each of these records has a numerical value in a column called 'weight' and I'm trying to sort this table so that for each ID the records are ranked in descending order of weight. This is going to be a one off process because I intend to make sure that data is sorted when it goes into the table in future.
Also is the normal protocol for doing something like this to SELECT all of the data, sort it the way you want, and then use the REPLACE command to replace the old data in the table?
I know that I can do this for one id by doing:
SELECT * FROM my_table WHERE id = 'X' ORDER BY weight DESC
but I need to somehow do this for each id in my table. How is this normally done?
you will ALWAYS return the data in the order you wish in this case:
SELECT * from theTable order by id, weight desc
you do not store the data in any particular order. (even if you try this will not matter).
Remove the WHERE clause and add id to ORDER BY. This way, your result set will be ordered first by id, and for each id, it will be ordered by weight.
SELECT * FROM my_table ORDER BY id, weight DESC
I have this part of a query :
GROUP BY trackid
ORDER BY MAX(history.date) DESC
And I see that, when I grouping, it returns the row with maximum date for each group. Why this behaviour? Order should works on the whole rows...not on the grouping ?!?!?
EDIT (Example)
This is my whole query :
SELECT tracklist.value, history.date
FROM history JOIN tracklist ON history.trackid=tracklist.trackid
ORDER BY history.date DESC
The result is :
tracklist3 2011-04-27 15:40:36
tracklist2 2011-04-27 13:15:43
tracklist2 2011-04-02 00:30:02
tracklist2 2011-04-01 14:20:12
tracklist1 2011-03-02 14:13:58
tracklist1 2011-03-01 12:11:50
As you can see, all line is correctly ordered by history.date.
Now I'd like to group them, keeping for each group the row with MAX history.date.
So the output should be :
tracklist3 2011-04-27 15:40:36
tracklist2 2011-04-27 13:15:43
tracklist1 2011-03-02 14:13:58
I see that :
GROUP BY trackid
ORDER BY MAX(history.date) DESC
works correctly, but I really don't understand why it works :) Order by is for the whole rows, not for the grouping....
When you say SELECT trackid, MAX(history.date) ... GROUP BY trackid ORDER BY MAX(history.date) DESC you are really saying: "Show me for each tracklist the most recent history entry and please show me the tracklist first whose most recent history entry is (overall) most recent."
The ORDER BY is applied after the grouping, (that's why it comes after the GROUP BY in the SQL).
Note that in your example, you have SELECT tracklist.value, history.date instead of SELECT tracklist.value, MAX(history.date). That is just wrong and unfortunately MySQL does not give a warning but it choses a random history.date at its discretion.
ORDER BY MAX(history.date) DESC is somewhat redundant if all you want to do is order the result set. Ordering applies to the result set.
Consider the results if you remove the ORDER BY clause. Without that, your query would only be grouping on the trackid column, so it wouldn't be valid--you would need to add an aggregate function to the date column or add the date column to the GROUP BY clause. By adding the aggregate function to the ORDER BY clause, you are essentially telling the SQL engine that for each group of trackid, get the maximum date. This seems to be what you want.
It seems you do not fully understand the GROUP BY statement. I would recommend looking up a tutorial on it.
But essentially, the GROUP BY statement combines a number of rows into one. The column names you GROUP BY determine how unique the new combined rows will be. SQL doesn't know how to handle all of the non-grouped columns since each new combined row will be pulling data from a number of rows that contain different values in these columns. That's why you use aggregate functions on the non-grouped columns in the SELECT statement. The aggregate function MAX() looks at all of the values in the history.date column for the rows that are being combined and returns only one of them. Additionally, the ORDER BY clause can only use columns that are being selected, that's why ORDER BY also must contain aggregate functions.