Halo all.
I have a table with two columns. One of them with customer_id and another one with acceptence_reason. I want to delete the customer_id from the acceptance part but have not yet come up with a effective way to do so.
To avoid confusion I have added a picture to clearify my problem
I suppose you already tried substr() but maybe you didn't know instr(). So I would try that:
select Customer_id, substr(acceptance_reason, 0, instr(acceptance_reason,"(") ) as acceptance_reason from ...
Related
Here is what I would like to do: I have an old vBulletin forum with a couple thousands of threads and replies. I developed a Laravel forum application myself and would like to migrate data from the old forum into the new one, even though the structure is completely different. What is more, in my new MySQL table I use base64 id's that I manually have to program. So I cannot use pure MySQL to fill this in I guess.
Essentially I need to do something like this:
Old table ---- new table
'thread' -> 'title'
'text' -> 'body'
and so on... plus the thing with the base64 id's.
Any idea how to approach this? I didn't quite find anything useful using search, probably because I wasn't looking for the right keywords. Thanks a lot!
Here's how to approach this kind of problem.
Start by doing a SELECT operation to generate a result set looking like your new table's columns. Maybe something like this will work for you.
SELECT thread AS title,
text AS body,
TO_BASE64(SHA2(UUID(), 224)) AS base64id
FROM old_table
(I guessed about what belongs in your base64id column. TO_BASE64(SHA2(UUID(), 224)) generates hard-to-guess pseudorandom values. You can use anything here.)
Once you're satisfied with your resultset, you can do
INSERT INTO new_table (title, body, base64id)
SELECT .... your select query
to put rows into the new table.
Hey guys am really new to mysql.I have heard of alias in mysql and i have tried double alias with the string like
select ('name' as bae,'age' as ages) as person;
When i run the above code it doesnt give me the output and raises the error.I dont understand why the double alias didnt works in mysql.
Any help to make this one correct would be really appreciated..Thanx in advance
The problem with your query are the parentheses. They represent a single expression and you don't put aliases (or commas usually) inside expressions. So, this fixes the immediate problem:
select 'name' as bae, 'age' as ages
I'm not sure what the second "as person" is supposed to be. Perhaps you want a subquery:
select person.*
from (select 'name' as bae, 'age' as ages) person
A really basic MySQL query should look something like:
SELECT
[COLUMN_NAME] AS [ALIAS_NAME]
FROM
[TABLE_NAME]
Based on your query it appears you are trying to retrieve the two columnns name and age from the person table.
If this is the case, the following query can help you:
SELECT
name AS bae,
age AS ages
FROM
person
But, this is still just one big guess, if you really want us to help you with your problems, you should give some more information about what you are trying to achieve.
I would like to know if it is safe to do something like this in MySQL queries:
I have a table level (id,label,etc)
SELECT level.label AS level FROM level
I mean
SELECT table_name.column2_name AS table_name FROM table_name
Actually I'm calling "level" table from another table by a "JOIN", but I made it simpler to post it; and it works!
But I wanted to know if this will not cause some issues later.
You should be perfectly fine doing this. I can make queries difficult to read - more complex ones than this, at any rate - so I wouldn't call it best practice, but if that's an appropriate name then use it. Another name might be LabelOfLevel which would get rid of the problem altogether....
Cheers -
Please help, I'm very confused about my situation.
I'm beginning to create a search function but I'm not sure the best way to go about it.
On the front-end users will be able to enter words in a text-field, then these will search the MYSQL database, something like below:
so they search for 'Adult' and every item_id (Primary Key) with 'Adult' in column 'name' is listed. Or they enter 'black' and every item_id with 'black' in 'colors' is listed. or they enter 'Adult Blue' and every item with either 'Adult' or Blue would come up. I'm sure you get the idea.
I've read up on multiple methods, but I can't figure out which is best:
Using a MANY TO ONE table: This seems like it would work, but there are over 500 unique items, so that would be thousands and thousands of rows. For item_id 1 I would have to make a row for 'Adult', a row for 'Denim', a row for 'Pants', a row for 'black', a row for 'red', a row for 'blue'. I might as well just hard code everything.
Using FIND_IN_SET: Is this going to work? Would I have to store the values with commas like Adult,Denim,Pants and also EXPLODE to separate the values? I was going to try this method but I keep reading that storing multiple values in a field is very bad practice.
Or are Regular Expressions what I'm looking for?
What is the best way to store the values, and what is the best way to retrieve them? I'm not asking for exact code, just the methods to use. Any advice is appreciated!
So, if we suppose that columns name and colors are the only columns we need to search through, I'd do the following (naive solution but will work fine if your DB doesn't have millions of rows and you don't have thousands of customers searching at once).
First, create a view
CREATE VIEW SearchHere AS
SELECT item_id, CONCAT(name, ' ', colors) AS FullDescription
FROM table
I don't know the name of the table in your screenshot, so I used table as its name.
Now, if a user searches for adult red pants you could issue a query
SELECT item_id
FROM SearchHere
WHERE FullDescription LIKE '%adult%'
AND FullDescription LIKE '%red%'
AND FullDescription LIKE '%pants%'
Of course, you'd need to generate the query on the fly but that's not an issue. You could play with using AND or OR and placing spaces in between the wildcrad symbol % and the search term. Probably you would also want to do the view in a more sophisticated way, e.g., do more tha just CONCAT.
A straightforward solution is to use
name REGEXP 'the|search|terms'
OR colors REGEXP 'the|search|terms'
You should explain what you mean by best, though -- fastest performance? easiest to maintain? other?
I have column in table where i store tag ids as 1|5|10
I want to explode the column using mysql query
What you probably might want to do is not store your tag ids like this. Make a separate table for them and your problem will dissapear naturally.
That's called normalisation.
You can chain SUBSTRING_INDEX to accomplish an explode effect, but let me remind you this is a horrible solution over database normalisation, as suggested by Kos.
If you were trying to select all MySQL entries with a 5 in your middle column (#|5|#) you would use this query:
SELECT * FROM table WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(table.field, '|', 2), '|', -1) = 5
#Everyone: this is a horrible solution so please don't PLAN to use it; however, some times we're in a situation where a quick one liner like this must be used until a finer inspection on the database schema can happen.