The mysql problems(select * from my table) - mysql

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`;

Related

In MySQL, what is the purpose of contains? can anyone give me it's usage with example?

I checked online resources for the use of contains in mysql, couldn't find anything. I tried the following query in my db.
select * from test_table where contains (column_a, 'a');, which returned
ERROR 3055 (HY000): Geometry byte string must be little endian..
You can use INSTR instead of CONTAINS.
SELECT * FROM test_table WHERE INSTR (column_a, 'a');
Mysql doesn't have contains keyword. If you want to achieve that you should use like keyword. Eg.
Select * from someTable where field like '%something%'

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.

Full JOIN MySQL Query is returning empty

So here is a MySQL Query:
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = 'LoggedCarts.Bill-Email'
LIMIT 0 , 30
It is returning an empty result set, when it should be returning four results based on the tables below.
First Table: LoggedCarts - Column: Bill-Email
casedilla#hotmail.com
crazyandy#theholeintheground.com
Second Table: TestSite - Column: email
samuel#lipsum.com
taco#flavoredkisses.com
honeybadger#dontcare.com
casedilla#hotmail.com
messingwith#sasquatch.com
The goal is to get a MySQL statement that returns the rows in Table: TestSite that don't match the rows in Table: LoggedCarts.
Note: I understand that the use of a hyphen in a column name requires special care when constructing a query, involving backticks to tell MySQL there are special characters. I would change the column names to match up, however the Table: LoggedCarts has data fed via post from a Yahoo Shopping Cart and without heavy preparation before insertion setting the name to anything but the key sent in the post data is daunting.
However, if it turns out rebuilding the data prior to insertion is easier than using a JOIN statement or for some reason using two columns with different names as the comparison columns just doesn't work, I will go through and rebuild the database and PHP code.
Single quotes indicate a string literal. You need to use backticks for identifiers. Also, each component of an identifier must be quoted individually.
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = LoggedCarts.`Bill-Email`
LIMIT 0 , 30
From the manual:
If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.
With a bit of research inspired by somne of the hints given, I found the solution I was looking for here: SELECT * WHERE NOT EXISTS
Does exactly what I need it to do, and as a bonus, I like the shorthand syntax that is used that allows you to put in an alias for the table name and use the alias throughout the statement.
SELECT *
FROM TestSite e
WHERE NOT EXISTS
(
SELECT null
FROM LoggedCarts d
WHERE d.`Bill-Email` = e.email
)

mysql won't order by INT

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

Simple WHERE clause for column name 'User ID'?

I am not able to run a simple select query with a where clause.
I am using MySQL and have a column User ID.
The problem is with the column name made of two words.
select * from user where 'User ID' = "xyz"
A usual query like the next one runs fine as expected:
select * from user where email = 'xyz'
How can I write a condition on the User ID column?
Try:
SELECT u.*
FROM `user` u
WHERE u.`User ID` = 'xyz';
But in general, try not to use such column names.
Using backticks to qualify the table and/or column names is also useful if you have names that conflict with MySQL keywords, e.g. user.
No way to rename this column ?
You can try with backticks around the column name in your query :
select * from user where `User ID` = 'xyz';
Like Jonathan Leffler pointed, if you are in MS SQL Server you can try:
select * from user where [User ID] = "xyz"
or in MySql PHP Admin:
select * from user where ´User ID´ = "xyz"
I am not sure about MySql, but in SQL Server, even "login" or "user" that are some reserved words, they are functional in queries normally. Despite, I think is better not to use that.
I hope this can help you or another one. Hugs.
Ref.:
1. Meaning of square brackets [] in MS-SQL table designer?
2. When to use single quotes, double quotes, and backticks in MySQL
I think user is a reserved word...
Try:
SELECT *
FROM `user`
WHERE `email` = 'xyz';