Home > Software engineering >  SQL 3 tables (long time ago)
SQL 3 tables (long time ago)

Time:02-15

I haven't done a SQL statement in about 10 years. A little help?

Table A:
ID, Title
1,"hello"
2,"world"

Table B:
ID, OBJ_ID
1,23
2,24

Table C:
ID, Name
23,"foo"
23,"bar"
24,"hi"
24,"pangea"

A.ID = B.ID
B.OBJ_ID = C.ID

One (A) to One (B) to Many (C)

Result needed:

A.Title, C.Name(s)
hello,foo,bar
world,hi,pangea

Thanks. I know I'm flat out asking for answer, but which JOIN would I use? Thanks. MariaDB, I'm trying to extract some data from someone's WP site. Why table B is there, is beyond me.

CodePudding user response:

Thanks @jarlh

select a.title, c.name from c join b on b.obj_id = c.id join a on a.id = b.obj_id

CodePudding user response:

SELECT A.Title, C.Name 
FROM A join B on A.ID=B.ID JOIN C ON B.ID=C.ID
ORDER BY A.Title, C.Name

If you want to have one row per title with all values from table C, consider using pivot.

  •  Tags:  
  • sql
  • Related