I have in my table this value ART(\'O\') in the field Subject.
How do I check if this subject exist?
I tried:
select * from table1 where Subject = 'ART(\'O\')';
select * from table1 where Subject = "ART(\'O\')";
Both failed at picking up the record.
How sholud I prhase the query so that the record containing ART(\'O\') will be picked?
Note: Please do not refer the query: select * from table1 where Subject like '%ART(%';
bec they may be other records such as ART(EX), ART(NA),etc... existing
Need to know how to use the Subject = '' method.
Thanks.
If the value contains the backslashes, you probably need to escape them. Otherwise you're looking for value ART('O').
SELECT * FROM table1 WHERE Subject = "ART(\\'O\\')";
Also make sure you don't have trailing whitespace.
Related
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
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.
I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it
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.
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';