Select statement not working on column name with possible preserved word - mysql

So I have a database filled with information on different columns and am trying some stuff out but started with just doing simple select all statements where a condition is true, such as:
'SELECT * FROM files WHERE ID = 2;'
my table name is called files and one of the columns is called ID which works fine. However, when working with my my column name called 'File', it doesn't work properly and I notice that it is blue like the other preserved words 'SELECT, FROM, and WHERE', so doing something like:
SELECT * FROM files WHERE File = 'example'; doesn't work even if example does exist in there, it just returns a blank result.
Is there a way that I can say that 'File' is a column and should be treated like one without having to re-name it to something else?
Any help would be appreciated! :)

MySQL Classes these as Reserved Words you can read more here: https://dev.mysql.com/doc/refman/8.0/en/keywords.html
To escape these words place ' around the word:
SELECT * FROM file WHERE `file` = 'example';

Related

Select a specific part of a value from a column in mariadb

i´m new in SQL and want to solve following case.
I have a table in a MariaDB named inventories. In this table are 2 columns: identifier and data.
I want to select and output a specific part in the column data, if the identifier is a specific one.
For example:
identifier
data
steam:xxxxxxxxxxxxxxx
...some data...,"Serial":"ZF3CJ8L1rrKjwP7nKTzb",...some more data...
The only part, that i want in the output is this: "Serial":"ZF3CJ8L1rrKjwP7nKTzb" but the part behind "Serial":" is a random value.
How can i solve this?
(i put a screenshot in here, table does not work for me in editorscreenshot of table in stackoverflow editor.)
You can try using the SubString function, which only returns a certain amount of character and starts a whatever character you want.
I got it!
SELECT (SUBSTRING(data, LOCATE('"Serial":"', DATA) + 0, 31)) FROM inventories where identifier = 'steam:xxxxxxxxxxxx';

SELECT Showing me a list with the name of the column and not the data inside

I have quite a strange problem, I'm very new to SQL and I was doing a free course in SQL
I'm actually learning the "SELECT" command.
So, I create my database, create some table, put some data in it with the "INSERT INTO" command
And now I want to select some data in it, but I have a strange bug (not an error) when i do
SELECT * FROM aliment;
everything work like it's supposed to do, but when I do
SELECT 'nom','calories' FROM aliment;
Something strange happens.
Instead have a list of all the specific data i'm looking to get i just get a list with all the data replaced by the name of the columns.
Here 2 screen to show you what's happens:
[2
I know one it's from the terminal(and yes it's not mine but from a video) and mine is from the software, but it's still had to work no?
You have a typo in your SQL. Use backticks (on the same key as ~ on a US keyboard) around your column names, not '. Using a single quote (an apostrophe) makes it a literal value, not a column name.
SELECT `nom`,`calories`FROM aliment;

How can i separate the data from selected data

I have a column in my table name as URL and it contains Multiple value like "https://www.google.com/#q=how+to+make+a+android+app"
and
http://www.bing.com/search?q=how+to+make+a+android+app&go=&qs=n&form=QBLH&pq=how+to+make+a+android+app&sc=8-15&sp=-1&sk=
I want to get data separately in output like
website = https://www.google.com
Keyword = how to make a android app.
Any Idea Plz, How can i get this in MySql.
you can use substr() function in php to cut string. In your case, you can get the characters up to the end of 'https://www.google.com'.
read more at http://ro1.php.net/substr
in case you want to do it directly in your query, you can use substr() as well. Sql syntax goes like this:
SELECT SUBSTR(column, start_position, desired_length) FROM table_name
*SELECT SUBSTR(column_name, 1, 20) FROM table_name;*

Creating variables and reusing within a mysql update query? possible?

I am struggling with this query and want to know if I am wasting my time and need to write a php script or is something like the following actually possible?
UPDATE my_table
SET #userid = user_id
AND SET filename('http://pathto/newfilename_'#userid'.jpg')
FROM my_table
WHERE filename
LIKE '%_%' AND filename
LIKE '%jpg'AND filename
NOT LIKE 'http%';
Basically I have 700 odd files that need renaming in the database as they do not match the filenames as I am changing system, they are called in the database.
The format is 2_gfhgfhf.jpg which translates to userid_randomjumble.jpg
But not all files in the database are in this format only about 700 out of thousands. So I want to identify names that contain _ but don't contain http (thats the correct format that I don't want to touch).
I can do that fine but now comes the tricky bit!!
I want to replace that file name userid_randomjumble.jpg with http://pathto/filename_userid.jpg So I want to set the column user_id in that row to a variable and insert it into my new filename.
The above doesn't work for obvious reasons but I am not sure if there is a way round what I'm trying to do. I have no idea if it's possible? Am I wasting my time with this and should I turn to PHP with mysql and stop being lazy? Or is there a way to get this to work?
Yes it is possible without the php. Here is a simple example
SET #a:=0;
SELECT * FROM table WHERE field_name = #a;
Yes you can do it using straightforward SQL:
UPDATE my_table
SET filename = CONCAT('http://pathto/newfilename_', userid, '.jpg')
WHERE filename LIKE '%\_%jpg'
AND filename NOT LIKE 'http%';
Notes:
No need for variables. Any columns of rows being updated may be referenced
In mysql, use CONCAT() to add text values together
With LIKE, an underscore (_) has a special meaning - it means "any single character". If you want to match a literal underscore, you must escape it with a backslash (\)
Your two LIKE predicates may be safely merged into one for a simpler query

MySQL - Finding partial strings - Full Text?

I just found a bunch of rogue data in my MYSQL db...
The only way to get to it is via one of the columns - FILE_PATH which contains a slash stripped version of a file path. There are a few rogue files in this set that I need find - they all have the file name "Thumbs.db" but they have a variety of paths
example:
F:DatasetGroupedByFormatsx-fmt-398Thumbs.db
I have a full text index on the field, however the following query doesn't give any returns:
SELECT * FROM main_small WHERE MATCH `FILE_PATH` AGAINST ('Thumbs.db')
Response:
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0382 sec )
I am unsure whether this is because I have the syntax wrong, or whether the text string needs to be isolated by whitespace/punctuation.
Surely it's
select * from main_small where FILE_PATH like '%Thumbs.db'
However, if not then does MySql Full text Search help?
The problem is that your query thinks 'Thumbs.db' is a whole word. You'll need to find some way to do wildcard searching in order to select those rows. How about:
SELECT * FROM main_small WHERE `FILE_PATH` LIKE '%Thumbs.db'
Just use LIKE:
SELECT * FROM main_small WHERE `FILE_PATH` LIKE '%Thumbs.db'