MySQL Forums
Forum List  »  Newbie

Re: Data from 2 tables
Posted by: Peter Brawley
Date: November 26, 2019 02:18PM

Including name.id values in the sex table is incorrect, you want ...

name(
  id int unsigned primary key auto_increment,
  firstname varchar(32),
  lastname varchar(32),
  sexid tinyint unsigned
) 

sex(
  id unsigned tinyint unsigned primary key, 
  name char(6) 
) 
with rows (1,'male'), (2, 'female'), (3, 'other')

... then a query for 'smith' males is ...

select n.lastname, s.name
from names n
join sex s on n.sexid=s.id
where n.last='smith';

Read about normalisation, then about joins.

Options: ReplyQuote


Subject
Written By
Posted
November 26, 2019 12:09PM
Re: Data from 2 tables
November 26, 2019 02:18PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.