select row from one table where value exists from array from another table

Dr J Manish

I have two tables.

  1. Users table have columns

    • member_group equals to
      • Admin
      • Marketing
      • Sales
    • zone equals to
      • North
      • West
      • East
  2. Meetings table have columns

    • for_member_group
    • for_zone columns.

for_member_group and for_zone may contain multiple values with comma separation.

E.g.

  • for_member_group = Admin,Sales
  • for_member_group = Admin,Marketing,Sales

    And same with for_zone column too.

Now I want to send emails to that particular meeting.

E.g. From Meetings table row 3 with values like for_member_group = 'Admin,Marketing' and 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

BUT USER QUERY is not working.... No user selected and no email gets sent...

I also tried 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
     }
 }
 ?>
Syscall

$for_member_group seems to be a CSV string. You have to quote all elements to use in IN() statement. Note that you should test if $for_member_group and $for_zone are not empty before to use them.

$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')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Joining row from one table with the sum value from another table

How to delete one row from table with one value where the table contains a second row with a different value?

Is it possible to join a SELECT from one table with another SELECT from other table in a single row?

Select a key value from MySQL table where one row with the key is greater than another

UPDATE one row in a table from a SELECT statement from another table

C# EF if row exists in table one select it else select from another table

Select from table where clause from array from another table

Select the row with the max value from a table where sum is calculated

How to select a value from one table where two separate pairs of conditions are met in another table

Select one or the other row from a table, depending on if other row exists

Select from one table where id (from another table) exists

SQL query to select from one table where either not in another table OR in that table with a specific value

MySQL select from a table and check in another table if the same value exists

How to add a value from one table to a particular row in another table?

Row value from another table

Select from table where column in select from another table in laravel

Select from one table and count from another

How to select items from a table based on one value if another value does not exists? (eloquent/sql)

SQL query to select row from one table which is not in another table

select from one table, count from another where id is not linked

Select all rows from a table except where row in another table with same id has a particular value in another column

If exists select column from another table

Select a value from a row that is matched to another value in the same table

Mark rows from one table where value exists in join table?

Sql select from a table but use another table value as where clause

Select Join From 2 table wich one of the table has array value of key from another table

Insert Values from one Table into Another table where foreign key value = value from other table

Check whether a value combination from one table exists in another table

If name exists in a table then return value from another column or row