I know this is a very common question. But I've looked through SO and google but I'm still unable to remove the first occurance or first few characters of a string which I retrieved from mysql.
I've got a table like this.
Name | Age
James| 27
My sql query (Select name from Human)
Once I execute the command from my bash script, it retrieves the row name (Name) and entry (James) in this form Name James when I only need James. How do I remove the word Name (including the space after it)? I want to store the result in a string and not echo it.
I think what you want is the mysql command line option --skip-column-names which will prevent the first line containing the column names to be returned with the SELECT query. Try that.
Related
I have a column in my SQL database (using mySQL) 'lastname', which has any number of employee's last names in it. I want to write a query that handles a search for last names using a comma delimited list.
So the user will type:
Garcia, Smith, Jones
And that gets stored in a String, lastNameQuery, which then gets passed into my backend API function that calls the SQL command. I want to return all employees in the DB that have those last names.
Is there any kind of SQL SELECT command I can use that search using a list like that? For my other functions (which only handle a single search term) I'm using this:
"SELECT * FROM employees WHERE salary LIKE '%${salary}%'"
Which works great. Is there some way I can modify it to handle a list? I can always break up the single String ("Garcia, Smith, Jones") into an array if necessary so that's not a problem. Any ideas?
You need to either do:
(lastname like '%Garcia%' or lastname like '%Smith%' or lastname like '%jones%')
or create a fulltext index on lastname (alter table employess add fulltext (lastname)) which would let you do
match (lastname) against ('Garcia, Smith, Jones')
but won't do things like find Garcia if you search for just "Garc".
Hope you all are doing well.
Today morning I was writing some sql query and I found a situation where I need your suggestion on that so here is the situation:
I've a table in mysql called users it has id column which is bigint
now when I'm trying to extract data with a query like :
select * from users where id = 123
in this case it'll show the result for user 123
now the situation here is if I run the same query like:
select * from users where id = 123b
now the issue here is it is still giving me the data for user 123
need your suggestion guys on the same, I did some R&D on the same but didn't found much usefull.
Thank you
Your question is tagged with mysqli which means you are using PHP to build your query. If you pass the string 123b as an integer, PHP will keep the leading digits:
var_dump((int)'123b');
# output: int(123)
MySQL then execute your query with this well formed integer 123.
If you execute a raw SQL query using any tool of your choice you should get an error like the following:
Unknown column '123b' in 'where clause'
This is because 123b cannot be an integer as it contains a letter, and isn't a string as it has no string delimiters.
Note that MySQL can still interpret strings without delimiters like hexadecimal values. For example, 0x31323362 is a valid string value for 123b and does not need quotes around it.
I'm using the following regex (https://regex101.com/r/Kt9sNj/1) in PHP to get all the files in the third level of a directory:
/^(\/[^\/]*){1,4}\/?$/m
Then if I have the following data:
/home/myuser/folder_example/first_file.txt
/home/myuser/folder_example/second_file.txt
/home/myuser/folder_example/third_file.txt
I get:
first_file.txt
second_file.txt
third_file.txt
I try to use this in a MySQL query that contains an array of a json object.
My Query is:
SELECT data->'$.files' AS File
FROM table
WHERE user = 'myuser';
And I get:
["/home/myuser/folder_example/first_file.txt","/home/myuser/folder_example/second_file.txt","/home/myuser/folder_example/third_file.txt"]
But when I use that regex on my sql query:
SELECT data->'$.files' AS File
FROM table
WHERE user = 'myuser'
AND data->'$.files' REGEXP '^(\/[^\/]*){1,4}\/?$';
I need to get this (all files under that directory):
["first_file.txt","second_file.txt","third_file.txt"]
It doesn't work. Do you know why?
The function REGEXP returns 1 if the pattern matches and will return the full match as the pattern does match the example strings.
In your pattern you are repeating a capturing group, which will capture the last value of the iteration in group 1, but it still contains a leading forward slash that you don't want in the output.
What you might do is match the first /, and then use a quantifier {3} to repeat exactly 3 times a part ending on a / using a non capture group.
Then capture the filename in group 1, and refer to that group using '$1' in the replacement using REGEXP_REPLACE
^/(?:[^/]*/){3}(\S+\.[^.\s]+)$
Regex demo | Mysql with replace demo
I have a column in my table that looks like this:
order_user
................................................
user#emial.com¬firstname lastname¬¬0888888848¬¬¬
Is there a query that can get only the first and last name and ignore everything else?
You can achieve this task using SUBSTRING_INDEX()
select substring_index(substring_index(order_user,"¬",2),"¬",-1) as Username
from yourTablename;
The SUBSTRING_INDEX() function returns the substring of string before number of occurrences of delimiter.
For more info, Click here
Feel free to ask doubt(s), if you have any.
I have a database full of important information... I need to change the information to fit a new format...
The current information follows this format... (Note this is all inserted in one cell)
DataString1:SomeOtherString1:MoreString1|DataString2:SomeOtherString2:MoreString2|DataString3:SomeOtherString3:MoreString3
I need to be able to do the following...
1) Locate all of the '|' symbols.
2) For each '|' symbol I need to find the second ':' before it.
3) Insert another ':' before the results of step 2.
I can accomplish this through code in another language like PHP for example but I would like to be able to do it via SQL.
The above example would turn into this... (I bolded the changes...)
DataString1::SomeOtherString1:MoreString1|DataString2::SomeOtherString2:MoreString2|DataString3::SomeOtherString3:MoreString3
I ended up just making a PHP script to do this, I'm not even sure what I wanted was entirely possible with MySQL as there was over 30 instances of | in each cell, and over 180 instances of ':' in each cell.... If anyone has an answer feel free to post and if it works Ill vote it as best answ
In sql the substring can be used like this substring(expression,startposition, length), If your occurence is repeating then you will have to create an update script which will check the substring postion at the character length lets say
DataString1
Here position of S is 5, you should count the occurence of | and save this on to a variable
select #sd substring (column_name ,start,length) from table where --conditons
Update table set column_name = '' where substring (column_name ,start,length) = '|'
you have to get the occurance of #sd and do the math to concatinate the string and update your cells
I ended up just making a PHP script to do this, I'm not even sure what I wanted was entirely possible with MySQL as there was over 30 instances of | in each cell, and over 180 instances of ':' in each cell.... If anyone has an answer feel free to post and if it works Ill vote it as best answer.