gpt4 book ai didi

php - 使用多个连接连接查询字符串

转载 作者:行者123 更新时间:2023-11-30 00:44:05 25 4
gpt4 key购买 nike

我对连接和基于字段动态生成它们还很陌生。

所有关键变量($et_key、$ct_key、$key)都是根据映射到数据库列名称的下拉值生成的。示例:

$state = $_POST['geography'];

$states = array('Alabama','Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District Of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts ', 'Michigan', 'Minnesota', 'Mississippi ', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming');

$key = array_search($state, $states);

if ($key !== false) {
$state_key = $key+1;
}

这是我的完整查询代码。我主要困惑的是如何处理所有“和”行——据我所知,第一行不能有“和”,但后续行必须有。我不确定如何解决这个问题。我还没有重构这个或任何东西,对于丑陋的重复感到抱歉。

$query=("SELECT * FROM exchange");

if($et_key) {
$query .= " INNER JOIN`exchange_type` on `exchange_type`.exchangeID = exchange.exchangeID ";
}

if($ct_key) {
$query .= " INNER JOIN `company_type` ON `company_type`.exchangeID = exchange.exchangeID ";
}

if($key) {
$query .= " INNER JOIN `exchange_states` on `exchange_states`.exchangeID = exchange.exchangeID ";
}

if ($et_key || $ct_key || $key) {
$query .= " where ";
}

if($et_key) {
$query .= " and `exchange_type`.$et_key=1 ";
}

if($ct_key) {
$query .= " and `company_type`.$ct_key = 1 ";
}

if($key) {
$query .= " and exchange_states`.stateID = $state_key ";
}


if (!empty($keywork)) {
$query .= "and (exchange.`exchange-name` LIKE '%$search%' OR exchange.`description` LIKE '%$search%')";
}


echo $query;

最佳答案

更改此:

if ($et_key || $ct_key || $key) {
$query .= " where ";
}

对此:

if ($et_key || $ct_key || $key) {
$query .= " where 1=1 ";
}

那么,“与”都有意义

关于php - 使用多个连接连接查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21533417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com