This question already has answers here:
How to remove new line characters from data rows in mysql?
(9 answers)
Closed 6 years ago.
I have a table Types that has the following columns: ID, Name, Type. The table is filled with about 300 rows. One of the rows:
ID Name Type
------------------
1 BMW S 1000 RR
The following query returns this row:
SELECT * FROM Types WHERE Name = 'BMW'
However, the following query returns nothing:
SELECT * FROM Types WHERE Type = 'S 1000 RR'
There are no extra spaces in the Type, and the data types of Name and Type are exactly the same (varchar 255, utf8_unicode_ci). What can possibly cause this?
I am using MySQL, InnoDB. Using phpMyAdmin I get the exact same results, so no typo's in column names...
I've found the problem: to fill the table I am reading a textfile per line. The newline character was the problem, it is invisible in phpMyAdmin's browse table view, but I saw it when editing a single row.
The following query fixed my problem:
UPDATE Types SET Type = REPLACE(REPLACE(Type, '\r', ''), '\n', '');
Found in How to remove new line characters from data rows in mysql?
Thanks everyone for your comments.
I am sure that's due to trim
Try this
SELECT * FROM Types WHERE TRIM(Type) = 'S 1000 RR'
Its not like that. Your table name and column names are all good and the query is also giving correct o/p:
go
CREATE TABLE Types
(
ID int,
Name varchar(3),
Type varchar(9));
go
INSERT INTO Types
(ID, Name, Type)
VALUES
(1, 'BMW', 'S 1000 RR')
SELECT * FROM Types WHERE Type = 'S 1000 RR'
Related
I've seen similar questions, but I don't seem to be missing a comma or anything, and TEXT is the value that corresponds with the database column.
I'm expecting to be able to submit a log consisting of text into my table, but I get the 1136 error instead. Removing the personal_log and (text) works fine, on submit I get an automated timestamp in the personal_log_date column and a NULL in the log column.
I'm very new to all of this and have read through some documentation, and it looks like TEXT should be accepted as a value.
Thanks for any knowledge you can drop on me!
$sql = "INSERT INTO `personal_log`(`personal_log_date`,`personal_log`)
VALUES (CURRENT_TIMESTAMP),(TEXT)";
This tries to insert 2 rows with 1 column:
VALUES (CURRENT_TIMESTAMP),(TEXT)
But you want to insert 1 row with 2 columns:
VALUES (CURRENT_TIMESTAMP, TEXT)
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.
My query is:
select
t.*,
e.emp_name,
d.department_name
from
task as t
LEFT JOIN employee as e on(e.emp_id=t.assign_to)
LEFT JOIN department as d on(d.depart_id=e.depart_id)
where
t.task_status='PENDING'
AND t.created_by!='31'
AND t.assign_to IN ('31')
order by
t.task_id DESC
The stored value in database
IN doesn't work like that
Example if your data looks like:
ManagerName, ManagerOf
'John', 'Steve,Sarah,Dave'
You can't do:
SELECT * FROM managers WHERE 'sarah' IN ManagerOf
IN is best conceived as an extension of OR:
SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
--is the same as:
SELECT * FROM managers WHERE
managerof = 'Sarah' OR
managerof = 'Steve'
There would be as many OR clauses as there are items in the IN list.
Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then
There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest
You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query
This question already has answers here:
Checking multiple columns for one value
(3 answers)
Closed 4 years ago.
I have a Database like the following:
I now want to check each row in this table if it contains a set of data. like so: I want to query for the following letters: A, B
this should return the following rows: res1, res4
this is because both res1 and res4 contain the values A and B in one of the columns
this logic also applies in this case:
and if i would only query for the letter B the query must return the following rows: res1, res2, res4
So what i want to happen is that the query checks the columns valsx, valsy, valsz, valsq if either one of these columns contains one of the queried values. And if all the queried values exist in the row (the four columns) then return the row.
How would i do this with SQL i have no idea. I tried
select * from table where valsx contains b or valsy contains b or valsz contains b or valsq contains b
if anything is unclear let me know so i can clarify.
You can use in:
select t.*
from t
where 'A' in (valsx, valsy, valsz, valsq) and
'B' in (valsx, valsy, valsz, valsq);
This question already has answers here:
How can I search within a table of comma-separated values?
(6 answers)
Closed 8 years ago.
For example, when a table has a record column named 'product' that contain value such as: 'Laptop, Desktop, Case'. How can I validate these 3 values that break down with a comma against two PHP variables value with $var1='Laptop' and $var2='Desktop' ? So that this row can be found! However, the two variables could be passed in the order of 'Desktop', 'Laptop' as well. Meanwhile, the column could have pattern of 'Case, Desktop, Laptop'. I wonder if there is a solution in MySQL for this kind of scenario that somehow, pick up each element like PHP could and match them with each var individually.
Without knowing anything about your table structure this is a quick example of what you can do.
SELECT * FROM table WHERE $var1 IN (SELECT product FROM table WHERE something = somethingelse) AND $var2 IN (SELECT product FROM table WHERE something = somethingelse)
As I understood, you want the data to be found, if the column 'product' contains 'Laptop' or 'Desktop'. Write this with the LIKE operator in your query:
"SELECT * FROM table WHERE `product` LIKE '%Desktop%' OR `product` LIKE '%Laptop%'"
If you pass the variables it would be:
"SELECT * FROM table WHERE `product` LIKE '%$var1%' OR `product` LIKE '%$var2%'"
Make sure to use the % sign before and after the searched string, so that it will match even if the keyword is anwhere inside the product content.
I have a table with an value that I would like to be updated periodically with a cron job. However, I need to update the value by replacing it with a value from a different table. The issue is that I would like the replacement value to be chosen randomly.
For example, Table 1 has
ID Email
=================
1 bobatumail
Table 2 has:
ID Email
================
1 bobatumail
2 joeatumail
3 peteatumail
4 biffatumail
5 wilneratumail
6 wilsonatumail
I would like the query to replace bobatumail in Table 1 with any of the other values in Table 2 as long as it is random. It could even be the same value as in Table 1.
Any idea how to do this?
In MySQL you could use the REPLACE statement:
REPLACE INTO table1 (ID, Email)
SELECT 1, table2.Email FROM table2 ORDER BY RAND() LIMIT 1;
The "1" in the second line represents the id of the entry while the second part returns a random value out of table2. Yes, there are solutions using the UPDATE statement (JOIN and ANSI) but its always tricky and you usually have to turn off safe update mode.
http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html#option_mysql_safe-updates
Please note that REPLACE first deletes the old entry and then reinserts the new one.
http://dev.mysql.com/doc/refman/5.5/en/replace.html