简体   繁体   English

从多列mysql中选择值

[英]select value from multiple columns mysql

On my site a user enters an account number and I have a script to show the business name belonging to that account number, however the account number could be in 2 columns. 在我的站点上,用户输入一个帐号,并且我有一个脚本来显示属于该帐号的公司名称,但是该帐号可以在2列中。

I have the below code to search 1 of the columns (personID); 我有下面的代码来搜索1列(personID);

sql="SELECT * FROM member WHERE personID = '".$q."'";

How do I edit this code to search 2 columns (personID & another column) for the account number? 如何编辑此代码以在2列(personID和另一列)中搜索帐号?

I believe the SQL OR is what you're looking for. 我相信您正在寻找SQL OR。 Might be worth taking a look here http://www.w3schools.com/sql/sql_and_or.asp 可能值得在这里看看http://www.w3schools.com/sql/sql_and_or.asp

sql="SELECT * FROM member WHERE personID = '".$q."' OR othercolumn = '".$q."'";

使用或:

sql="SELECT * FROM member WHERE personID = '".$q."' OR anotherColumn = '".$q."'";

You can do this with in : 你可以这样做in

sql="SELECT * FROM member WHERE '".$q."' IN (personID, othercol)"

The advantage of in is that the parameter is only mentioned once. in的优点是该参数仅被提及一次。 This makes it easier to use proper parameters rather than string substitution -- a much better approach to generating SQL. 这使使用适当的参数而不是字符串替换更容易-这是生成SQL的更好的方法。

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

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