![]() ![]() It's going to cast it as an unsigned data type which means it's going to look at it like a number and that's what unsigned represents. What this is going to do is it's going to bring these values in and instead of treating them just like alphanumeric characters. Here I'm going to say cast as unsigned and then we'll do ascending. So what casting does is it gives you the ability to change the datatype on the fly of the values coming in. ![]() The way that you can fix that is by doing what's called CASTing. Probably not what you'd expect for something like revenue But there are going to be times when you have to deal with that. Right here guides_revenue is set to a varchar. Where the item in that column data attribute isn't what you'd expect. I want you to see that are going to be many times when you're working with things like legacy data applications. This is a cool thing and this is part of the reason why we have this set up with this type of data type. This doesn't really make any sense because it's trying to just take all of the items and it's treating them like alphanumeric values, even though they're really numbers. So the way this is it's not really from a revenue perspective. You can see that we have this integer with 11 VARchar right here. So if we go to our tables for guides and if I look at the schema inspector in columns. However guides_revenue If you remember if you look at the schema, the way that we structured this was a little bit different. The issue is that when we try to order by something as alphanumeric like guides_title that works perfectly fine. When I was first learning SQL this part was pretty confusing to me. This is definitely not what you'd expect. Now If I switch this to ascending and process this you can see that it has more weird behavior. If we're sorting by descending we would expect to be able to see the largest numbers all the way down to the smallest ones. So by default the way that MySQL works is it's going to process this and it's going to say 750,750, 500, 1300, and then a 1000. However I want you to notice something a little bit weird here. Now if I run this query it will work in regards to it doesn't throw an error. Then I actually want to sort by revenue and I'll try to do it descending. If I paste this in and say that I want the guides_title but even before that I want to have the guides_revenue. Now I want to show you something that may seem a little weird. So if I run it again now you can see that it is alphabetically sorted. If I were to copy this and sort ascending, It's going to be the same results set. Now if I run this code you can see that now this is sorted this and it has “something else”, “My Great post”, and “my blog”. So here I'm going to say guides_title and then you say if you want it to be descending or ascending. So in order to do that we're going to add another line and say “ORDER BY” and then you pick out the parameter that you want to order it by. However, we also have the ability to add a ORDER BY clause which will allow us to sort the data as it comes through and the result set. So if I run this code you can see that it has “my blog”, “something else”, “My Great post”, “my blog”, and “my blog” and that may be fine. The results are going to just sort it by the id that's the default that SQL runs. For example right now if I were to just say SELECT guides_title FROM guides and if I run this query it's going to bring back in all the results. One of the ways it can be formatted is you can see things in a specific order. In many cases when you're working with SQL queries it's very important to have some formatted data when it comes back.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |