I have the following MySQL query:
SELECT * FROM members_family_view ORDER BY `agelastsept` ASC
Which returns the following results:
I want to be able to change the data returned for display purposes so that instead of agelastsept displaying 7 it would display U8's, 8 would display U9's, 10 would display U11's, 11 would display U12's, 12 would display U13's and 13 would display U14's.
Is this possible in a MySQL query?
try this query
select concat('U', (id+1), '\'s') as Name, total from tbl
SQL FIDDLE:
| NAME | TOTAL |
----------------
| U2's | 50 |
| U3's | 55 |
| U4's | 89 |
You simply need to concatenate:
SELECT
CONCAT("U", agelastsept + 1, "'s") AS 'agelastsept',
total
FROM members_family_view
ORDER BY `agelastsept` ASC
Select CONCAT("U", (agelastsept + 1 ), "'s") as agelastsept,total FROM members_family_view ORDER BY `agelastsept` ASC
Related
I want to make a selection in my MySQL database where I have stored numerical values in a specific column.
I have seen some websites that show 10 ten based on some criteria, and I've been wondering if it will be possible to make a selection based on numerical values.
I have my database like so:
| id | name | pts |
+----+-----------+-----+
| 1 | Brad | 3 |
| 2 | Jane | 8 |
| 3 | Jones | 10 |
| 4 | Paty | 15 |
| 5 | Sammy | 2 |
Now my question is, how do I make a query that selects only the top 3 users, based on the pts, such that it returns the result as:
1st Position = Paty => 15pts
2nd Position = Jones => 10pts
3rd Position = Jane => 8pts
?
try this :
SELECT * FROM tablename ORDER BY pts desc limit 3
Your query should use LIMIT to get the top results:
SELECT id, name, points FROM table ORDER BY pts DESC LIMIT 3
shoul do the trick.
Order by will order the table from the highest to the lowest and limit will tell mysql to get only the first three results
You can find more on this topic here for example.
And this is a question very close to your
you can use this query
SELECT CONCAT(`id` , 'Postion = ' , `name` , '=>' ,`pts` , 'pts' ) AS result FROM table ORDER BY pts DESC LIMIT 3
This Question I seen many places anyway the query is.
select top 3 * from tablename order by cols_name desc
This Query brings top 3 row with highest values based on your column.
I have a problem when selecting and grouping the following table.
The table "Igralci" looks like this:
----------------------------------------------
|ID|U_ID|st_tock|st_srecanj|st_nizov|st_gemov|
----------------------------------------------
|19| 17 | 6 | 3 | 6 | 72 |
----------------------------------------------
|11| 19 | 12 | 6 | 24 | 144 |
----------------------------------------------
|15| 18 | 12 | 6 | 26 | 72 |
----------------------------------------------
I would like to sort the id's in the following way:
1. First looks at st_tock (if st_tock is same) ->
2. Looks at st_srecanj (if st_srecanj is same) ->
3. Looks at st_nizov (if st_nizov is same) ->
4. Looks at st_gemov
I tried:
Select * from Igralci
group by id, st_tock, st_srecanj, st_nizov, st_gemov
order by st_tock, st_srecanj, st_nizov, st_gemov;
The wanted display would be (ID's):
15 ( same st_tock and st_srecanj as 11 but he has more st_nizov)
11
19
You need to add "Desc" or "asc" to your order by clause to get the results you want. Based on the output you provided, you need to add "Desc" after each column in the order by
Select * from Igralci
group by id, st_tock, st_srecanj, st_nizov, st_gemov
order by st_tock desc, st_srecanj desc, st_nizov desc, st_gemov desc;
I've two queries which should both return thumbUrls in the same order. My goal is to use the second query, as it has to be part of another query, which won't work with the first one. I already broke down the queries, but couldn't find what's casing the difference.
Query One:
SELECT album, thumbUrl
FROM lychee_photos
WHERE album = '16'
ORDER BY takestamp ASC, id DESC LIMIT 3;
Result:
-------------------------------------------------
| album | thumbUrl |
-------------------------------------------------
| 16 | 48c4d04567590b44a0c604197a049ef5.jpeg |
| 16 | 5db0fce4937104e7d27dae64d6cd9990.jpeg |
| 16 | a70bb8499b683b1eeab17336c3f07f7e.jpeg |
-------------------------------------------------
Query Two:
SELECT album, substring_index(
GROUP_CONCAT(thumbUrl ORDER BY takestamp ASC, id DESC), ',', 3
) AS thumbs
FROM lychee_photos
WHERE album='16'
GROUP BY album;
Result:
-----------------------------------------------------------------------------------------------------------------------------
| album | thumbUrl |
-----------------------------------------------------------------------------------------------------------------------------
| 16 | 1b98e17f1af051753a958c18953adbd5.jpeg,c506f4fedb8a4df691d276d6abdb75f7.jpeg,f0cc0ba23effc0443f367ca63dd1e72f.jpeg |
-----------------------------------------------------------------------------------------------------------------------------
Expected output:
I expected to see thumbUrls in the same order as in query one.
-----------------------------------------------------------------------------------------------------------------------------
| album | thumbUrl |
-----------------------------------------------------------------------------------------------------------------------------
| 16 | 48c4d04567590b44a0c604197a049ef5.jpeg,5db0fce4937104e7d27dae64d6cd9990.jpeg,a70bb8499b683b1eeab17336c3f07f7e.jpeg |
-----------------------------------------------------------------------------------------------------------------------------
Additional details:
Data hasn't changed between the queries
Removing substring_index() doesn't affect the order
The correct thumbUrls appear in the middle of query two, when I remove substring_index()
The first query returns correctly sorted thumbUrls—the second don't. But why? How do I keep the output format of the second one and get a correct sorting?
What's the best way to find the absolute difference between two numbers in MYSQL so that I may order results? The below works, only if numberA is larger than numberB, but as you can see this is not always the case. Is there a good way to do this with one statement?
SELECT (numberA - numberB) AS spread
FROM table
ORDER BY spread DESC
|-------------------|
| numberA | numberB |
| 5.4 | 2.2 |
| 7.7 | 4.3 |
| 1 | 6.5 |
| 2.3 | 10.8 |
| 4.5 | 4.5 |
As simple as that:
SELECT ABS(numberA - numberB) AS spread
FROM table
ORDER BY spread DESC
Or, if you want to select the pair (numberA, numberB) in descending order of their difference:
SELECT numberA, numberB
FROM table
ORDER BY ABS(numberA - numberB) DESC
MySQL has an ABS() function for that:
select abs(numberA - numberB) as abs_diff
from your_table
ORDER BY abs_diff DESC
I my mysql db I have a table with 3 parameters ( name, views, id ). I need to get row ordered by views. I'm getting something like this.
query:
select
from table
order by views
Result:
id | name | views
------------------------
7 | xxxx | 9000
2 | yyyy | 8000
1 | aaaa | 7000
4 | bbbb | 6000
8 | dddd | 5000
6 | cccc | 4000
5 | oooo | 3000
3 | tttt | 2000
What I need to do, is to get rows ordered by views but starting with specyfic ID. Is it possible. All input that i have is ID. Let sat that ID is 6, this should be output:
id | name | views
------------------------
6 | cccc | 4000
5 | oooo | 3000
3 | tttt | 2000
I can't use LIMIT as I don't really know what is possition at the moment. I just need to get rows which are left starting with ID.
What I'm trying to do is to get infinite scroll, I requesting next elements base on last element that was displayed. Only tricky part is that I'm ordering by views parameter.
select * from table
where (views = 4000 and id>6) or (views < 4000)
order by views desc, id asc;
The tricky part is that you have to know (select) the views of the element with ID 6; also you need to use the ID as secondary sort criteria in order to get consistent results.
Actually this is a common case of a since,until type of paging
SELECT * FROM table
WHERE views <= (SELECT views FROM table WHERE id = 6)
ORDER BY views