Let's say that I have the following code:
SELECT * FROM table where company LIKE '%Auto%'
And I receive more results, and I want to have an option to sort the results alphabetically, let's say that the user wants to sort the search results for the ones which start with "C"!
Best Regards,
The MySQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
If you want to select records from a table but would like to see them sorted according to a given column, you can simply use the ORDER BY clause at the end of a SELECT statement. It doesn't matter how complicated or long your SQL query is— ORDER BY should always be at the end of the command.
The SQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
Well, it seems that you are talking about two different things. If you are interested in sorting you would need to use the ORDER BY clause:
SELECT * FROM table ORDER BY name
If you want to filter the results by items that start with the letter 'C' then you would want to add another LIKE clause with that letter:
SELECT * FROM table where company LIKE '%Auto%' AND name LIKE 'C%'
Additionally you'll notice that the name filter only has the %
after the query. This is the syntax for "starts with"
Use the ORDER BY
clause:
SELECT *
FROM table
where company LIKE '%Auto%'
order by company
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With