Welcome Guest, Not a member yet? Register   Sign In
Matching data from 2 table with validation date
#1

(This post was last modified: 02-06-2019, 01:08 PM by DELE.)

At this time I have data from the database, results of the join data table users and table campaigns, like this :

ID
001
005
008

and I have data from table incomes, like this :

id       name      date
1        001        2018-10-10
2        005        2018-10-10
3        008        2018-10-10
4        001        2018-10-11

how to check the results of the join data in table incomes?

if the data ID is not the same as the data name on 2018-10-11, the results will be obtained :

ID
005
008

The question: what query should I use and what method do I need to apply in the controller / view. if the masters here are willing to include with simple code it is very very valuable to me.

thank you, greetings coding.
Reply
#2

@DELE,

You need to determine what join you want (https://www.cloudways.com/blog/wp-conten...ysql-1.png )

This might work for you...

select ucj.id
from users_campaigns_join ucj
inner join incomes i on i.id = ucj.id
where i.date = '2018-10-10'
Reply
#3

(This post was last modified: 02-07-2019, 03:47 PM by DELE.)

(02-06-2019, 07:45 PM)php_rocs Wrote: @DELE,

You need to determine what join you want (https://www.cloudways.com/blog/wp-conten...ysql-1.png )

This might work for you...

select ucj.id
from users_campaigns_join ucj
inner join incomes i on i.id = ucj.id
where i.date = '2018-10-10'

from users_campaigns_join ucj // this is not a table but results from a combination of user tables and campaign tables, are you sure this way?
Reply
#4

(This post was last modified: 02-07-2019, 07:36 PM by php_rocs.)

@DELE,

users_campaigns_join could be a view or a query (subset of data).  I just used the name to represent the data.

Like below...
PHP Code:
select ucj.id
from users_campaigns_join_view ucj
inner join incomes i on i
.id ucj.id
where i
.date '2018-10-10' 
Reply
#5

(This post was last modified: 02-08-2019, 08:34 AM by DELE.)

(02-07-2019, 04:35 PM)php_rocs Wrote: @DELE,

users_campaigns_join could be a view or a query (subset of data).  I just used the name to represent the data.

Like below...
PHP Code:
select ucj.id
from users_campaigns_join_view ucj
inner join incomes i on i
.id ucj.id
where i
.date '2018-10-10' 

I do not really understand your point.

this is the code that I use to produce (what you call) ucj.
PHP Code:
public function test1()
{
    $this->db->select();
    $this->db->from('users');
    $this->db->join('campaigns''campaigns.id = users.id');
    $res $this->db->get();

    foreach ($res->result() as $key) {
 
      echo $key->ID.'<br>';
    }
}

// Results
// 00001
// 00005
// 00008 

how to implement your method using this method?
PHP Code:
public function test2()
{
    $this->db->select();
    $this->db->from('???'); // how to implement your method here?
    $this->db->join('incomes''incomes.id = ???.id');
    $this->db->where(???);

    $res $this->db->get();
    foreach ($res->result() as $key) {
 
      echo $key->ID.'<br>';
    }
}

// So the results can be like this
// 005
// 008 
Reply
#6

(This post was last modified: 02-08-2019, 08:50 AM by php_rocs.)

@DELE,

I prefer to use the query bindings feature ( https://www.codeigniter.com/userguide3/d...y-bindings ).   This gives me more flexibility to write my own queries and the binding feature automatically escapes the variables for me.  So my code would look like...

PHP Code:
$sql "select ucj.id
from users_campaigns_join_view ucj
inner join incomes i on i.id = ucj.id
where i.date = ?"
;

$this->db->query($sql, ['2018-10-10']); 
Reply
#7

I understand the meaning of the flow of your code, which I am confused is the users_campaigns_join_view.

because what I know is that it must contain the name of the database table that will be joined.
Reply
#8

@DELE,

Are you familiar with views? Not CI views but database views?
Reply
#9

(This post was last modified: 02-09-2019, 08:29 AM by DELE.)

(02-08-2019, 09:19 AM)php_rocs Wrote: @DELE,

Are you familiar with views?  Not CI views but database views?


thank you very much for your help, my knowledge in the database is still ordinary. but now I have got what I want.

I look for differences between two tables.

thank you again for your help.


PHP Code:
public function test()
{
 
   $t $this->db->query("
        SELECT *
        FROM ( 
            SELECT users.id
            FROM users
            INNER JOIN campaigns
            ON users.id = campaigns.id
        ) users_campaigns
        WHERE NOT EXISTS ( 
            SELECT *
            FROM incomes
            WHERE users_campaigns.id = incomes.income_campaign
            AND incomes.income_date = '2019-02-02'
        )
    "
);

 
   print_r($t->result());
}

// RESULTS
// ID
// 005
// 008 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB