简体   繁体   English

MySQL连接两个表的where子句

[英]Mysql join two tables where clause

So I'm listing some activities based on a form where the user select the country, date, etc.. There's two tables, the main one "actividades" is where I have the name, price, number of participants, etc and then I have "tarifas" where I put special rules for activities, for example if on a specific day that activity is available or not. 因此,我根据用户选择国家/地区,日期等形式列出了一些活动。有两个表,其中一个主要的“活动”是我的名字,价格,参与者人数等,然后我有“塔里法斯”,我在其中为活动设置了特殊规则,例如,在特定日期是否可以进行活动。 there could be a rule or not, if there's not any rule, I want to list that activity, if there's a rule, I only want to list it if that day is availabe which is what tarifas.fechado = 0 is supposed to do. 是否有规则,如果没有规则,我想列出该活动,如果有规则,我只想列出当日可用的情况,这就是tarifas.fechado = 0应该做的。

Everything else is working, can someone help me sort this out? 其他一切都在工作,有人可以帮我解决这个问题吗?

if (!$sql = $db->sql_query("SELECT actividades.dias, actividades.id_pais, actividades.id_regiao, actividades.nome, actividades.id_empresa, actividades.id, actividades.dias, actividades.preco, actividades.id_subcategoria, actividades.hora 
                            FROM actividades 
                            LEFT JOIN tarifas ON actividades.id = tarifas.id_actividade 
                                              AND tarifas.dia = '$dia' 
                                              AND tarifas.fechado = 0 
                            WHERE pais = '$pais' 
                            AND actividades.dias LIKE '%$diaExtenso%' 
                            ORDER BY actividades.id ASC 
                            LIMIT $pageLimit, $porpagina")) 
       {message_die("DS", TRUE); }

I think some of your join conditions need to be in the WHERE clause instead. 我认为您的某些加入条件必须在WHERE子句中。 I'm not clear on how tarifas.dia fits into the picture, but this query will return all actividades rows that have no related tarifas row, plus all those that do have a tarifas row for which tarifas.dia has the specified value and tarifas.fechado is zero. 我不清楚tarifas.dia如何适合图片,但是此查询将返回所有不具有相关tarifas行的actividades行,以及所有确实具有tarifastarifas.dia具有指定值和tarifas.fechado为零。 (I have formatted it for readability): (为了方便阅读,我对其进行了格式化):

SELECT
  actividades.dias,
  actividades.id_pais,
  actividades.id_regiao,
  actividades.nome,
  actividades.id_empresa,
  actividades.id,
  actividades.dias,
  actividades.preco,
  actividades.id_subcategoria,
  actividades.hora
FROM
  actividades
  LEFT JOIN tarifas
    ON actividades.id = tarifas.id_actividade
WHERE
  pais = '$pais'
  AND actividades.dias LIKE '%$diaExtenso%'
  AND (tarifas.id_actividade IS NULL
    OR (tarifas.dia = '$dia' AND tarifas.fechado = 0))
ORDER BY actividades.id ASC
LIMIT $pageLimit, $porpagina

It is possible that you want to move the condition on tarifas.dia back into the join predicate. 您可能要将tarifas.dia上的条件tarifas.dia回连接谓词。 Doing so would cause tarifas rows to be ignored unless they have the specified dia value. 这样做将导致tarifas行被忽略,除非它们具有指定的dia值。

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

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