doumuyu0837 2016-10-08 05:16
浏览 36

使用php mysql显示外键值

I have 3 tables which are states, cities, colleges. All are contains id and corresponding names. I have one more table students with column names are 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..

  • 写回答

1条回答 默认 最新

  • donglang8008 2016-10-08 06:06
    关注

    use sql join assuming state_id, city_id, college_id are saved for each student for ex.

    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';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理