Cultureshock Posted November 19, 2009 Share Posted November 19, 2009 How might I take a set of data from table 1 and apply it to table 2? For instance. Table 1: id*, name, age Table 2: id, pet How would I be able to view all the pets of people who are 18 (if 'id' is unique in table 1 and shared by all the pets owned by that person in table 2)? does that make sense? >.< Quote Link to comment https://forums.phpfreaks.com/topic/182186-apply-set-of-data-to-another-table/ Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Simply add a referencing ID as a foreign key in Table 2 to Table 1: Table 2: id, personid, pet Assuming that person identifies the first table correctly, change person to be part of table 1's actual name. Quote Link to comment https://forums.phpfreaks.com/topic/182186-apply-set-of-data-to-another-table/#findComment-961292 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2009 Share Posted November 19, 2009 For your example, combined with premiso's, you could retrieve the information like this... SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.personid WHERE table1.age = 18 That SHOULD give you results you expect after you've renamed the table names. It's been a while since I've had to work with SQL (embarrassingly enough), so my knowledge of these things could be a bit rusty, but you get the general idea - joins are your friend. Quote Link to comment https://forums.phpfreaks.com/topic/182186-apply-set-of-data-to-another-table/#findComment-961304 Share on other sites More sharing options...
Cultureshock Posted November 20, 2009 Author Share Posted November 20, 2009 But how would that help if I have multiple ages? (i.e. grab all the pets from table 2 who's owners in table 1 are 18, 19, 23, or 9.) The data searched for has to be dynamic. Quote Link to comment https://forums.phpfreaks.com/topic/182186-apply-set-of-data-to-another-table/#findComment-962131 Share on other sites More sharing options...
premiso Posted November 20, 2009 Share Posted November 20, 2009 SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.personid WHERE table1.age IN(18, 19, 23,9) ORDER BY table1.age Simple as that. Quote Link to comment https://forums.phpfreaks.com/topic/182186-apply-set-of-data-to-another-table/#findComment-962168 Share on other sites More sharing options...
Zane Posted November 20, 2009 Share Posted November 20, 2009 Using your example it would look like this Table 1: id, name, age, pet Table 2: id, pet Then you would do SELECT b.pet FROM table1 AS a INNER JOIN table2 as b ON a.pet = b.id WHERE a.age = 18 Quote Link to comment https://forums.phpfreaks.com/topic/182186-apply-set-of-data-to-another-table/#findComment-962226 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.