简体   繁体   中英

problem relating two tables for get same id mysql

I have problems to relate the columns of two tables

this is my table1 structure

where I want to relate the identify column to the id of table 2这是我的 table1 结构

this is my table2 structure

这是我的表2结构

this is my query

$id             = (!empty($_GET['id']) ? $_GET['id'] : 0);
    $example1       = (!empty($_REQUEST['example1']) ? $_REQUEST['example1'] : '');
    $example2 = (!empty($_REQUEST['example2']) ? $_REQUEST['example2'] : '');
    $consult  = "SELECT a.id, a.user, a.date, a.action
    FROM table1 a
    INNER JOIN table2 b on b.idwork = $id
    WHERE module = 'Activity ".$idexample1."|".$idexample2."' AND identifier = $id ORDER BY id DESC ";

I get this after run my query

SELECT a.id, a.user, a.date, a.action 
        FROM table1 a
        INNER JOIN table2 b on b.id = 244
        WHERE module = 'activity 1|98' AND identificador = 244 ORDER BY id DESC 

what I try is to relate and make is that $id be the same identifier number

The join should be based ON the equality of the columns b.id and a.identifier :

SELECT a.id, a.user, a.date, a.action 
FROM table1 a INNER JOIN table2 b 
ON b.id = a.identifier
WHERE a.module = 'activity 1|98' AND b.idwork = 244 
ORDER BY a.identifier DESC 

I used also the WHERE clause from your code and changed the column names to the ones of your sample data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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