简体   繁体   English

从一个表中选择行,其中从另一个表中的数组中存在值

[英]select row from one table where value exists from array from another table

I have two tables. 我有两张桌子。

  1. Users table have columns Users table有列

    • member_group equals to member_group等于
      • Admin 管理员
      • Marketing 市场行销
      • Sales 营业额
    • zone equals to zone等于
      • North
      • West 西方
      • East
  2. Meetings table have columns Meetings table有列

    • for_member_group
    • for_zone columns. for_zone列。

for_member_group and for_zone may contain multiple values with comma separation. for_member_groupfor_zone可能包含多个逗号分隔的值。

Eg 例如

  • for_member_group = Admin,Sales for_member_group =管理员,销售
  • for_member_group = Admin,Marketing,Sales for_member_group =管理员,市场营销,销售

    And same with for_zone column too. for_zone列相同。

Now I want to send emails to that particular meeting. 现在,我想向该特定会议发送电子邮件。

Eg From Meetings table row 3 with values like for_member_group = 'Admin,Marketing' and for_zone ='East,West' 例如,从会议表第3行开始,其值类似于for_member_group ='Admin,Marketing'和for_zone ='East,West'

Now mail should be sent to Admin and Marketing name containing rows and East and West containing zone names only form Users Table 现在,仅应从Users Table中将邮件发送到包含行的Admin和Marketing名称以及包含区域名称的East和West区域

BUT USER QUERY is not working.... No user selected and no email gets sent... 但是用户查询不起作用。...未选择用户,也未发送电子邮件...

I also tried echo $userdata['email']; 我也试过echo $userdata['email']; But nothing echoed...and No Error is seen... 但是没有回声...并且没有看到错误...

I have tried as follows : 我尝试如下:

<?php
    $qry = "select * from $meetings where meeting_start > $today order by meeting_start desc limit 1";
    $results = $database->get_results($qry);
    foreach($results as $lecture_data){
        $for_member_group = $lecture_data['for_member_group'];
        $for_zone = $lecture_data['for_zone'];
        $userquery = "select * from $users where member_group IN('$for_member_group') and zone IN('$for_zone') ";
        $userresult = $database->get_results($userquery);
        foreach ($userresult as $userdata){
            echo $userdata['email'];
            ob_start();
   ?>

   <!-- EMail HTML and PHP Coding-->


 <?php

  rest php code
     }
 }
 ?>

我遇到了同样的错误,请尝试执行此查询。

$userquery = "select * from $users where member_group IN(".$for_member_group.") and zone IN (".$for_zone.") ";

I think you have to use FIND_IN_SET() 我认为您必须使用FIND_IN_SET()

Just like below: 就像下面这样:

$userquery = "select * from $users where (FIND_IN_SET('Admin',member_group) OR FIND_IN_SET('Sales',member_group) ) and (FIND_IN_SET('North',zone) OR FIND_IN_SET('West',zone))";

$for_member_group seems to be a CSV string. $for_member_group似乎是CSV字符串。 You have to quote all elements to use in IN() statement. 您必须引用所有要在IN()语句中使用的元素。 Note that you should test if $for_member_group and $for_zone are not empty before to use them. 请注意,在使用它们之前,应测试$for_member_group$for_zone是否不为空。

$mem_quote = "'".implode("','", explode(',', $for_member_group))."'";
$zon_quote = "'".implode("','", explode(',', $for_zone))."'";
// $mem_quote will be equals to 'Admin','Marketing'
$userquery = "select * from $users 
              where member_group in($mem_quote) 
                and zone in($zon_quote)";

The query will look like: 查询如下所示:

select * from $users 
 where member_group in('Admin','Marketing') 
   and zone in('East','West')

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

相关问题 从存在ID(从另一表)的一个表中选择 - Select from one table where id (from another table) exists Select 全部来自一个表,但如果它存在于另一个表中则删除 - Select all from one table but remove if it exists from another table Select from table where 子句 from array from another table - Select from table where clause from array from another table MySQL从表WHERE值&gt; 0中选择行 - MySQL Select Row from table WHERE value > 0 SELECT * FROM table WHERE value IN array 只返回一个结果 - SELECT * FROM table WHERE value IN array only return one result 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table 如果另一个表中存在值,请从表中全选 - Select all from table if a value exists in another table MySQL从一个表中选择并检查另一个表是否存在相同的值 - MySQL select from a table and check in another table if the same value exists Mysql选择具有相同值的FROM table_one WHERE列中的任何一行出现多次,然后从Array中的table_two输出其数据 - Mysql select Any one row FROM table_one WHERE columns having the same value occurs more than once, then output their data from table_two in an Array 从一个表中选择计数,另一个表中有一个位置 - Select count from a table with a Where on another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM