Select and display by a value of a number - mysql

I want to select from my database and to display each value by a value of a number, example:
I have in database this:
Peter 3.80
Maria 5.67
John 2.52
Robert 1.53
I want to display this values but I want to display by example Maria show more times than Robert because she have value of number 5.67 and Robert have 1.53
Output must be Names like Maria, John...
At every refresh show me one name but I need frequency with value number high

I think you are looking for order by. Just use order by col_name, here you may wanna add desc for descending order

Are you searching for sorting the name by the value of number ?
You can try use
SELECT name
FROM your table name
ORDER BY your column name DESC;
You can see reference here to for this Mysql Sort

Related

How to group by with 2 different field for the below criteria

Name date sellingAmt
Raju 08-02-2017 2000
Ravi 09-02-2017 5000
Ravi 09-02-2017 2000
Raju 09-02-2107 1000
Expected Result
Name date sellingAmt
Raju 08-02-2017 2000
Ravi 09-02-2017 7000
Raju 09-02-2107 1000
Let me know how to group by this in mysql select query
Seems like a pretty basic question. What part are you struggling with?
Always post what you've tried so we can look at your train of thought and try to correct the problem at it's root instead of giving you a baked solution!
Date (and I just see name is as well) is a keyword/reserved word so out of habit I use the ` to escape them.
I used sellingAmt Desc as the order by since it appears you want those within the same date; though you could just as easily want name desc.
I group by name, date to achieve the desired results. While mySQL extends the group by so you can group on fewer values than what is in the select, I find this feature is wrongly used more often than correctly. So I tend to error on the side of caution and always group by all fields not part of an aggregate; which is what other RDBMS systems would require of you. In your case date and name are the unique combination to sum sellingAmt correctly and achieve your expected results.
.
SELECT `Name`, `Date`, Sum(SellingAmt) sellingAmt
FROM {tblName}
GROUP BY `Name`, `Date`
ORDER BY `date`, sellingAmt Desc
Replace {tblname} with your table name.

Find lowest value and coresponding code on asingle query mysql

I have a user table as follows
id name age
1 John 21
2. Mathan 23
3. Raj 21
4. Manoj 50
5 Krishnan 91
I want to find minimum age and its corresponding name. How can I do it with rails?
Can I do it in a single query?
Note: More than one names can have single age.
Is there a specific reason why you want to do it in a single query ?
If you can write 2 queries, I think you can just write :
User.where age: User.minimum(:age)
select age, group_concat(name) from table group by age order by age asc limit 1
You will need to process the results later on in ruby, but this gives all you need in one single query. Also i am assuming mysql, so might differ on other rdbms.
It gives exact output in mysql that you want try this
SELECT concat("[",name," ",age,"]") AS name
FROM TABLE
WHERE age =
(SELECT min(age)
FROM TABLE);

Counting the number of pattern occurrences in a certain column with MySQL

I want to search the column Name of a certain table to see how many rows have a Name value that matches a certain pattern. For example, if the pattern I am looking for is %Peter% and in this table there are 5 rows with the Name values:
Peter
Peter Smith
George Peter
Peter Peter
Carl
I want to obtain the value 4. I tried to use COUNT, but don't know how to combine it for example with LIKE. How do I go about to do this?
Not sure why didn't LIKE work for you, but this SHOULD do the trick:
SELECT COUNT(*) FROM table
WHERE name LIKE '%Peter%'

mysql select 6th thru 10th (natural order) records

I have a simple table:
ID | Name
0183 namez
2543 etc
2654 etc
4364 namez
3246 namey
3745 namew
3464 namem
7524 etc
2459
2457
0845
9325
I need to be able to select the 6th thru 10th rows or the 4th thru 25th or whatever, so that I can select only the rows that I need without using any kind of Id column, also it's alway Xth "thru" Yth, because I'm not hardcoding an column names here, I can't use order by but have to use natural order. Is this even possible? Thanks for any help.
you need to pass a LIMIT clause to your SELECT query. In MySQL this would be:
SELECT * FROM simpletable LIMIT 5, 5;
NOTE:
the first number is the offset, it needs to be the first row minus one, (i.e. 6 - 1).
the second is the number of rows returned, this needs to be last row - offset (i.e. 10 - 5).
SEE: http://dev.mysql.com/doc/refman/5.0/en/select.html
SELECT *
FROM tables
ORDER BY ID
LIMIT 5, 5
You can do the following:
SELECT * FROM tables ORDER BY ID LIMIT 4, 10

Mysql sorting using numbers and characters

I am having a mysql table field with mixed values.Say,
1
2
Priya
Radha
If I sorts this in ascending order, the result will be displayed as shown above and if I sorts in descending order, the result will be
Radha
Priya
2
1
Here Mysql will give priority to numbers and then characters.
What to do, if I want Mysql to give priority to characters?
ie, Sorting in ascending order should give a result like the one below:
Priya
Radha
1
2
Please help
I found this, maybe you can try it.
Sorting characters and numbers in mysql