简体   繁体   English

使用PHP MySQL显示外键值

[英]Displaying foreign key values using php mysql

I have 3 tables which are states , cities , colleges . 我有3个表,分别是statescitiescolleges All are contains id and corresponding names. 全部包含id和相应的名称。 I have one more table students with column names are id, studentname, state, city, college . 我还有一个表的students ,其列名分别是id, studentname, state, city, college

Now i need to search how many students are from selected state/city/college, . 现在,我需要从选定的州/城市/学院搜索多少学生。 sometimes i need to select multiple options like state and city but not college or state college but not city etc... 有时我需要选择多个选项,例如城市,而不是大学或州立大学,而不是城市等...

My query is: 我的查询是:

SELECT `state`, `city`, `college`, `student name` FROM `students` where `state`='1' AND `city`='4';

It returns records but state city college columns will be foreign keys, i need to see city name but not city id, state name but not state id, etc.. 它返回记录,但state city college列将是外键,我需要查看城市名称而不是城市ID,州名称而不是州ID等。

使用sql join假设为每个学生保存了ex的state_id,city_id,college_id。

SELECT `states.*`, `cities.*`, `colleges.*`, `students.*` FROM `students` left join states on states.id = students.state_id left join cities on cities.id = students.city_id left join colleges on colleges.id = students.id where `students.state`='1' AND `students.city`='4';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM