mysql won't order by INT - mysql

I have set up a table with a column called order. I made sure it is set as INT. I can't seem to get the results to list in order. I currently have 5 rows with numbers 1-5 in a random order in those rows. However, I can't get those rows to go in order. Maybe I am doing this completely wrong, as I am new to MySql. Here is my query
SELECT * FROM faq ORDER BY 'order'

You should use the back-tick, not the quote:
SELECT * FROM faq ORDER BY `order`

You need to use backticks in mysql, not quotes.
SELECT * FROM faq ORDER BY `order`

You need:
SELECT * FROM faq ORDER BY `order`
You're using single-quotes in your example. MySQL uses backticks for wrapping table names, field names, etc. You need to use backticks in this case because order is a reserved word in MySQL.

You're quoting 'order' like a string, so the sorting will be done by the value order itself (a string) rather than by the column. Change them to backticks instead.

You should use backtik not quotes:
SELECT * FROM faq ORDER BY `order`;
Refer: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

Related

SELECT query with WHERE returns all rows when column name and value are equal

I am confused by how my SELECT query is behaving. Accidentally I read the header row of a .csv file into my table. This means there is now one row in the table which has the column values in each corresponding column.
But a SELECT like this
select * from `mytablename` where segmentering=`segmentering`;
Returns all the rows in the table.
Why is MySQL ignoring the condition?
it should be 'segmentering' not with back tick(``)
select * from `mytablename` where segmentering='segmentering'
Problem is you use single quote ` object identifier for strings.
instead of it use normal single quote:
select * from `mytablename` where segmentering='segmentering';
I guess the problem is with you are adding back quotes(``) with the value field. Back quote will be used for specifying column or table names. It should not be used with the value.
Try using,
select * from `mytablename` where `segmentering`='segmentering';
OR
select * from `mytablename` where segmentering='segmentering';
Try this
select * from `mytablename` where segmentering='segmentering';
Remove `` and replace '';
I hope it will work for you

MySQL When to use quotation after SELECT AS

After reviewing the use of SELECT in mysql, I found after as, sometimes without single quotation and sometimes has.
For example:
SELECT * AS DAY
compare to:
SELECT * AS 'Cancellation Rate'
So when to use single quotation after SELECT AS?
for composite name eg: Cancellation Rate.. use backtics not quotes
select my_col_name as `Cancellation Rate`
from my_table
The proper syntax would be something like:
SELECT column_name AS colname FROM table_name
As mentioned in the comment, you cannot not alias a 'select all', which is what * represents. It selects ALL columns from your table.
You can also alias a table's name, like:
SELECT * FROM employees e WHERE column_name = 1;
When you alias a table's name, it can be easier to read in larger and more complex queries such as Joins.
You can get a better idea of all the possibilities by exploring this page https://dev.mysql.com/doc/refman/8.0/en/select.html, plenty of fairly easy to follow examples.

The mysql problems(select * from my table)

I want to get a column from my table, but something wrong.
enter image description here
enter image description here
This problem is strange, I select * from order(a table of my database), but I cannot do that, I don't know where is wrong.
order is a reserved keyword in MySql where it is generally used to order output with reference to particular coloumn.
General logic of any coding or database structure is to avoid reserved keywords by that particular coding language or databases.
check the following link for reserved keywords in mysql
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
In your case to resolve the issue , simply use back tick (" ` ") for the word order i.e.
select * from `order`
order is a reserved word in SQL. You will need to escape such a table name in your SQL:
SELECT * FROM `order`;
But it would be better would be to not use that as a table name.
Change the name of the table order or use `` before and after it so the code will be:
SELECT * FROM `order`;

How do I select columns which has a dot in their column names

I am trying to select a record which clearly exists, but my SQL query does not bring it up. Any idea how to get this working?
SELECT * FROM Users WHERE 'local.email'='burgundy#email.com' LIMIT 1
The issue is that you're using single quotes ( ' ) around your column name, rather than using backticks ( ` ).
Try using this instead:
SELECT *
FROM Users
WHERE `local.email` = 'burgundy#email.com'
LIMIT 1
Like Crocodile said, anything that is a SQL variable like a table name or column name can also be surrounded by `` Back ticks (hold shift and hit ~). This tells SQL to look at them as literals.

MySQL multiple IN clause does not work

I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,