I have a table ML, with column Klubs:
I want to split this column so I have two columns like this. New table name ML2 and column name Region.
First column can have 2 words if they are different from 2nd column word.
I am new to MySQL and searched all over stackoverflow and internet , but no luck.
How can split column Klubs from table ML as in 1st picture into new table ML2 with 2 columns as in 2nd picture?
Assuming that your second column does not include any spaces, we can search for the location of the last space, and then split there:
SELECT Klubs,
substr(Klubs, 1, char_length(Klubs) - instr(reverse(Klubs),' ')) as Region,
substr(Klubs, char_length(Klubs) - instr(reverse(Klubs),' ')+2) as Club
FROM ML;
I search for the the space with instr, and to do this reverse I use the reverse function on the string first, and then to compensate I subtract it from the length of the string with char_length.
You could insert it in a new table (ML2):
create table ML2
<the query>
or create a view:
create view ML2 as
<the query>
Related
I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;
I have a question about replace function (or anything similar to that).
I have a query that returns more than 1500 results and one of the columns is a 'BODY' column which includes inside sql query.
The columns of the returned table are: Id,name, date, title, body.
In the body column i have sql query such as:
"select * from tbl.student s inner join tbl.teacher t on s.id = t.sId
where title like '%Joni%'"
And than I have a separate list of 60 tables which some of them for example: tbl.student, tbl.teacher, tbl.class, tbl.school and etc.
I want to run on all 1500 results that iv'e got, look for each result if body field contains one of the tables that in the list (tbl.student, tbl.teacher, tbl.class, tbl.school ...
) and if so i want to replace tbl. to abc.
For example, if one of the results contains in its body field tbl.student I want to replace it to abc.student
What will be the most effective way to do that?
Thanks!
I have a database table that contains person's fingerprints (template column here), each person can introduce 2 fingers, so the same person must have 2 records.
Here is the table :
Person with ID '275' have 2 records, each for a single finger.
Now I am using Talend to create a table so I can merge every two fingerprints in a single one, I mean row n°37 and 38 will be in single row and template column will be concatenated to have only one person_id
You can try something like this:
INSERT INTO newtable (ids, person_id_integer)
SELECT CONCAT(finger1.id, "|", finger2.id), finger1.person_id_integer
FROM oldtable finger1, oldtable finger2
where finger1.person_id_integer = finger2.person_id_integer
and finger1.id <> finger2.id
Of course, depnds on how you want to store the new data
:)
I found a solution :
I added the same table twice as an input and output like shown in this photo and first fetched it with this query :
select FP_ID, FP_DEVICE, group_concat(FP_FINGER_PRINT SEPARATOR ''),FP_EMPLOYEE_ID
from t_finger_print GROUP BY FP_EMPLOYEE_ID;
lets take an example - i have 2 data tables, table "books" with columns "shelfId" and "text", and table "shelves" with column "Id". I want to join these two tables on books.shelfId == shelves.Id, and as a result, i want to see a new table with 2 columns - column 1 has unique values of Ids, and column 2 has merged values of books.text with same books.shelfId values and separated by comma or something else, i.e. :
Is it possible to write such sql select to get what i need ?
Here is fiddle http://sqlfiddle.com/#!2/c96dfa/1
SELECT shelfid as id, GROUP_CONCAT(text) AS text
FROM books
GROUP BY shelfid
I have two tables:
table_people
col_name
col_sex
table_gender
col_male
col_female
Suppose the table_people consist three rows, (a,'M'),(b,'M'),(c,'F').
Now I need a query(subquery) to insert this first table value in second table as:
(a,c),(b,'').
If it is possible in mysql?
You table structure is bad.
My suggestion is to drop table_gender because it doesn't make sense at all. You already have a list of person with gender on table table_people.
You can generate a VIEW, if you want to list the gender separately.
CREATE VIEW MaleList
AS
SELECT col_name
FROM table_people
WHERE col_sex = 'M'
and another view for list of females only.
CREATE VIEW FemaleList
AS
SELECT col_name
FROM table_people
WHERE col_sex = 'F'
First of all the table structure is bad. I dont see a point of saving M or F in another table when you col_sex in your table_people. Still if you want to do it, first you have to specify a foreign key connecting the two tables.