sql - How to do a MySQL select query on two tables linked by another table -


suppose have table function link 2 other tables in terms of oop.

suppose have 2 tables: 1 person's name , 1 phone numbers:

table 1: id   person's name 1    john 2    smith  table 2: id   phone number 5     23424224 6      23424242 

and have third table links person , respective phone numbers:

table 3: id    person-id    phone-number-id 1         1           5 2         2           6 

hence john has phone number 23424224 , smith has phone number 23424242.

and want run sql query fetch persons table 1 phone number start with, let's say, (234).

how go linking select queries within table structure...what query run?

first, reason table if have many-to-many relation. while person can have many phone numbers, can 1 phone number have many persons? if true, schema implements requirement, seems little over-engineered me :-)

second, simple join. want first select out phone numbers in question, given that, select out person ids third table, given that, select names first table. like:

select t1.name name, t2.number table1 t1, table2 t2, table3 t3 t2.number '234%' , t3.personid = t1.id , t3.phoneid = t2.id;

you can rewrite "blah.id = blah.id" join if need outer join semantics (include fields nulls).


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -