1

I use mysql 5.1.72-community.

I have a table named team:

team_id          team_name
    1             US
    1             CH
    2             AD
    2             JP
    2             IT
    3             AU
    3             CL
    3             CU

and a table named person:

person_id        address
    10051        US. Idaho
    10152        US. Connecticut
    10053        CH. Aargau
    10054        CH. Bern
    10055        JP. Tokyo
    10056        JP. Ginza
    10057        Uneo of JP

Now I want create a view named person_view, like:

person_id     address        team_id
10051        US. Idaho          1
10152        US. Connecticut    1
10053        CH. Aargau         1
10054        CH. Bern           1
10055        JP. Tokyo          2
10056        JP. Ginza          2
10057        Uneo of JP         2

That means if person.address contains the team.team_name, then person_view.team_id = team.team_id.

How can I do this please?

6
  • 1
    Have a team_id column in your persons table. Commented Jan 24, 2018 at 10:41
  • @jarlh Thank you. that is an old project, have no person.team_id. I just want to do that by a view. Commented Jan 24, 2018 at 10:44
  • @xunitc Is the team_name in the adress column always on the left or right side? Or can it be anywhere in the adress column? Commented Jan 24, 2018 at 10:49
  • @ppijnenburg i didnt see 10057 at the time of posting thanks for pointing that out, i have deleted my answer Commented Jan 24, 2018 at 10:50
  • @ppijnenburg it can be anywhere in the address. Thank you, I see your first answer, it find some rows except 10057. Commented Jan 24, 2018 at 11:00

3 Answers 3

1

Have the team_id column in persons table if you can and join on it. If you can't - you can try doing this:

CREATE VIEW person_view
AS
SELECT p.person_id,
    t.address,  
    t.team_id
FROM person p 
-- if team name can be anywhere in the address:
JOIN team t ON p.address LIKE CONCAT('%', t.team_name, '%');
-- -- if team name will always be in front of address:
-- JOIN team t ON p.address LIKE CONCAT(t.team_name, '%');
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I try it in mysql 5.1.72-community, but it can not run. I just copy it and run. what is my wrong please?
@xunitc , my bad. I've updated code to use CONCAT instead of + now.
0

Try this (sorry I can't test it):

SELECT p.person_id, p.address, t.team_id
FROM team t, person p
WHERE t.team_name like p.address

3 Comments

Thank you, but it can not work for me, maybe I have some mistakes.
check column names, table names spells
Thank you, and that need p.address like t.team_name, and % needed too.
0
CREATE OR REPLACE VIEW person_view
AS
SELECT p.person_id,
    t.address,  
    t.team_id
FROM person p ,
team t where p.address LIKE '%' || t.team_name || '%';

Try this

3 Comments

Thank you, but the view is too large by this sql. maybe Cartesian product.
Is it containing duplicates?
Yes. it will put 10053,"CH. Aargau" to team_id 1,2,3, and term_id 1 twice and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.