我的查询有问题。
我有一个具有多种选择值类型的搜索表单。一些过滤器是单选选择,其中一些是多个选择值(强制性)。
我的问题是当我有多项选择并且需要在查询子句WHERE
中使用LIKE AND OR
时。
我使用动态查询PDO构建了结果页面,并且WHERE
子句被内爆并与AND
连接,但是我不知道如何添加OR
,因为对于使用多个选择。
我在Result.php上的代码是:
if (isset($_POST["submit"])) {
$conditions = [];
$parameters = [];
if (isset($_POST["reg"])) {
$conditions[] = 'region = ? ';
$parameters[] = isset($_POST['reg']) ? $_POST['reg'] : '' ;
}
if(isset($_POST["paravilw"])) {
$conditions[] = 'stt_rsw = ? ';
$parameters[] = isset($_POST['paravilw']) ? $_POST['paravilw'] : '' ;
}
if (isset($_POST["pros_tre"])) {
$prev_table ='" OR pros_tresit LIKE "';
$value_rank = $_POST["pros_tre"];
$value_rank_implode = "" . implode("%" .$prev_table."%", $_POST['pros_tre']) .""; - ---i tried to use implode but is not return if i have multi selection
$value_rank_explode = explode('|', $value_rank_implode);
$conditions[] = ' pros_tresit LIKE ? ';
$parameters[] = '%'.$value_rank_implode.'%';
//$parameters[] = '%' . implode("%' or pros_tresit like '%", $_POST['pros_tre']) .'%';
var_dump($parameters);
//print_r($parameters);
}
$sql = "SELECT * FROM import ";
if ($conditions)
{
$sql .= " WHERE " .implode(" AND ", $conditions);
}
$stmt = $pdo->prepare($sql);
$stmt->execute($parameters);
$data = $stmt->fetchAll();
$total_row = $stmt->rowCount();
$output = '';
}
现在,我将解释我尝试做的事情。我使用implode方法在每个选定的新值上手动添加OR pros_tresit LIKE
。
$prev_table ='" OR pros_tresit LIKE "';
$value_rank_implode = "" . implode("%" .$prev_table."%", $_POST['pros_tre']) .""; - ---i tried to use implode but is not return if i have multi selection
但不是返回数据,我的var_dump($sql)
结果是
SELECT * FROM import WHERE region = ? AND stt_rsw= ? AND pros_tresit LIKE ?
和我的var_dump($parameters);
是:
array(3) { [0]=> string(2) "NOS" [1]=> string(4) "TRUS" [2]=> string(43) "%Sil%" OR pros_tresit LIKE "%Unr%"
如果复制粘贴最后一个数组[2]并在我的数据库中手动运行,它将工作并返回数据 变成这样:
SELECT * FROM import WHERE region = 'NOS' AND stt_rsw = 'TRUS' AND pros_tresit LIKE "%Sil%" OR pros_tresit LIKE "%Unr%"
我不知道如何通过多选OR table_name LIKE
解决此问题
你能帮我吗?
答案 0 :(得分:0)
伙计们,我找到了一种可以起作用的方法,但不是我期望的那样
private fun addRow(content: Editable) {
val tl = findViewById<TableLayout>(R.id.main_table)
val margin = TableRow(this)
val tr_head = TableRow(this)
margin.id = View.generateViewId()
tr_head.id = View.generateViewId()
val tabParams = TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
84
)
tr_head.setLayoutParams(tabParams)
val label = TextView(this)
label.id = View.generateViewId()
label.text = content
label.setTextColor(Color.WHITE)
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
label.width = (displayMetrics.widthPixels / 3)*2
label.setPadding(50, 0,0,0)
tr_head.addView(label)
val btn = Button(this)
btn.id = View.generateViewId()
btn.width = (displayMetrics.widthPixels / 3) - 50
btn.setBackgroundColor(Color.RED)
btn.setText("Remove")
btn.setOnClickListener {
tl.removeView(tr_head)
tl.removeView(margin)
}
label.id = View.generateViewId()
val filler = TextView(this)
filler.id = View.generateViewId()
margin.addView(filler)
tr_head.addView(btn)
tl.addView(
margin, TableLayout.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
20
)
)
tl.addView(
tr_head, tabParams
)
}
我在这里做了类似的事情,其余的代码在所有条件下都能很好地工作
if (isset($_POST["pros_tre"])) {
$prev_table ='" OR pros_tresit LIKE "';
$value_rank = $_POST["pros_tre"];
$value_rank_implode = "" . implode("%" .$prev_table."%", $_POST['pros_tre']) .""; - ---i tried to use implode but is not return if i have multi selection
$value_rank_explode = explode('|', $value_rank_implode);
$conditions[] = ' pros_tresit LIKE ? ';
$parameters[] = '%'.$value_rank_implode.'%';
//$parameters[] = '%' . implode("%' or pros_tresit like '%", $_POST['pros_tre']) .'%';
var_dump($parameters);
//print_r($parameters);
}
我要感谢GitHub上的'noahheck',因为我安装在我的Project中
当您有很多查询并且需要查看数据在SQL中的位置时,它非常有用。 例如,此功能可帮助我调试查询语句。
我知道这不是一个完美的解决方案,但是暂时它可能会在我将获得有关PHP的更多知识后的几周或几个月内起作用,
感谢stackoverflow