douba1498 2014-04-21 11:07
浏览 24

任何人都可以给我正确的方法来编写这个函数的查询吗?

I'm trying to implement search function where the user can search either of the following: name, age, address, gender, course and year?

SELECT *
FROM student
WHERE name LIKE '%name%'
  OR age BETWEEN 'age' AND 'age'
  OR address LIKE '%address%'
  OR gender = 'gender'
  OR course LIKE '%course%'
  OR YEAR = 'year'


Note: each of the search criteria is in different input field

but i can't get the correct results whenever the user input either 1 or more of the search criteria.

i know when to use the OR and AND but I just can't get the correct way of writing query for that.

example is: i have table student.

id   name   address   age   gender  course  year

2    juan   New York   19    male   IT      2
4    whitney New Jersey 23    female BA     4
7    manny   Canada     20    male   Law    2

Note: age is a select input field. example is 
15-20
20-25
25-30 and etc.

however when I'm trying to search for example is course which is IT, it returns all records which is wrong. the correct result should only be related to IT like the first record.

  • 写回答

1条回答 默认 最新

  • dongren1353 2014-04-21 11:13
    关注

    I think this is what you want

    SELECT * FROM student WHERE name LIKE '%name%'  OR (age > 23 AND age < 30)
    OR address LIKE '%address%'  OR gender = 'gender' OR course LIKE '%course%' 
    OR YEAR = 'year'
    

    Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀