I came across a scenario where i have values in a coloumn like
Buno foo
Buno this
Buno that
Buno bar
This is the values of a single coloumn,Now i want to sort this coloumn excluding the
Buno
word,means sorting should be applied on foo,this,that and bar.Is there a way to do this in MySql?
Select * from tableName order by Column_Name
This query can get you the required results, you dont have to worry about the first word.
database query sorts on the first word, if the first word is same, then its sorts according to the second word.
If I am not mistaken, using ORDER BY will sort the entire value in the column, not just the first word, so you should just be able to use ORDER BY to solve your problem...
If you wanted to remove the first section though, you would need to use SUBSTRING in your SELECT and then ORDER BY your substring...
EDIT: Saw your comment above on not knowing how to use SUBSTRING, here is an example using your data:
SELECT SUBSTRING('Buno Foo', 5) AS NOBuno FROM MyTable ORDER BY NOBuno ASC
You can extract the substring of your column from the 6th character onwards (since you always have Buno at the front, which is 5 characters long). To do this use SUBSTRING:
SELECT ...
FROM ...
ORDER BY SUBSTRING(my_column FROM 6)
Note that SELECT SUBSTRING(my_column FROM 6) will return foo,this,that, etc.
If you wanted to be a bit more general and order using the second word, you can try SUBSTRING_INDEX. (read the docs and you'll work it out).
Related
Got stuck on this one, know it should be simple.
But I have a list of unique IDs that looks like AB123456, XY584234, CDE987654. The last six characters mean something, so I need to find all rows that have the same last six characters as another (substring).
So ABCD1234 would match XYCD1234, and return the both of them. Need to run this on the whole database and get all the matches, preferably with the matches next to each other.
Is that possible?
You can do this with group by and right. The following returns a list of all ids that look similar:
select right(id, 6), group_concat(id)
from table t
group by right(id, 6);
You might want to add:
having count(*) > 1
If you don't want singletons.
Please use below query to get your result.
select * from tablename where right(columnname,6)= value
I have a group of records that look like these following notional results. There are multiple occurrences where the number in the count column is repeated. 'count' comes from char_length(text_column). I want to grab the first record in each such grouping. How do I do this?
I've looked at the char_length() documentation and searched for examples where this has been done, but can't find anything.
select distinct(char_length(text_column)), text_column from table
.... is as close -- which is not obviously close -- as I've been able to get to the result.
=======+============================
count + text_column
=======+============================
2139 + some text entry
-------+----------------------------
3000 + another sentence here
-------+----------------------------
3000 + more text in this sentence
-------+----------------------------
3000 + still more text here
-------+----------------------------
Thank you,
BW
There is no such thing as "first" unless a column specifies the ordering. You can, however, get a value from an indeterminate row by using group by:
select char_length(text_column), text_column
from table
group by char_length(text_column);
I'm not a big fan of using the MySQL extension to group by where text_column is allowed in the select, but not the group by. However, because it is a long column, it might be more efficient to just let MySQL pick the value rather than using an actual aggregation function (such as min() or max()).
I am using the test data "bank" to study mysql on mac. I have a question about the alphabet sort in mysql.
I have a example codeselect cust_id,cust_type_cd,city,state,fed_id from customer order by 2 asc;
The return shows in column 2, "I" is before "B".
Anyone knows what is the reason? Many thanks.
I would guess that cust_type_cd is an ENUM column with "I" ordered before "B" in the enum definition.
Enums sort by the ordinal position of the value in the list defined by the enumeration, not by the alphabetical value.
To sort alphabetically, either define the enum with entries in alphabetical order, or else force the value to be converted to its string value:
... ORDER BY CONCAT(cust_type_cd) ASC
See also http://dev.mysql.com/doc/refman/5.6/en/enum.html#enum-sorting
Note that using a function like that in the ORDER BY clause spoils any chance of using an index for sorting. It will be forced to use a filesort.
Use below Query. It seems there is some space before I character.
select cust_id,trim(cust_type_cd) cust_type_cd,city,state,fed_id from customer order by 2 asc
Using order by column numbers is strictly not recommended. It is especially not used when SELECT * is not used with this. Also it will create problems when somebody alters the table, adds/removes some columns. This link might help you http://blog.sqlauthority.com/2010/12/27/sql-server-order-by-columnname-vs-order-by-columnnumber/
You have to give order by column name in the table
Suppose if you want to sort according to cust_id you have to use
select cust_id,cust_type_cd,city,state,fed_id from customer order by cust_id asc;
I have a table with "unique" values. The problem is that the program, which adds these values also adds 3 different postfixs to the value (2 characters in the end of the value). As a result, I have three variable with three postfixs. So i need get only unique values from bd - somehow sort it out without the last two characters. Are any ideas?
What Camera_id should you return (first,last,maximum,minimum???) if rows have one "unique" value but different Camera_id's. Try something like this:
select
LEFT(camera_name,LENGTH(camera_name)-2), max(camera_id)
from cameras
where site_id=1
group by LEFT(camera_name,LENGTH(camera_name)-2)
Do you want to retrieve the values with the first letter only?
SELECT DISTINCT SUBSTRING(ColumnName, 1,1) a
FROM tablename
ORDER BY a
can you show sample records? it helps a lot when your asking question.
This appears to work,
SELECT CONCATENATE(col1,col2) newcol,sum(othercol)
FROM mytable GROUP BY newcol.
Or even
SELECT STR_TO_DATE("%Y%m%d") as newcol,sum(othercol)
FROM mytable GROUP BY newcol.
Looking at these, one assumes the first example produces a count for each unique combination of col1,col2 as a string, the second example to produce a count for each day.
Having been bitten before by mysql e.g. silently ignoring "missing" columns in group by,
Does the above actually work, or are there any hidden gotchas ?
If you want to count rows, you should use count(*) instead of sum().
Otherwise, this pattern works fine.
(I personally use "GROUP BY 1" to signify grouping by the first column).