简体   繁体   中英

Enter data from a different mysql table

My current query is:

INSERT INTO `ipdKayaChikitsaF`(`IPDNo`, `OPDNo`, `RegNo`, `PatientId`, `PDep`, `PWard`, `PBed`, `pSymptom`, `PDisease`, `PName`, `PMob`, `uid`, `PSex`, `PAge`, `PMStatus`, `PFname`, `PAddr`, `PDist`, `PState`, `OPDDate`, `dischargeStatus`, `admitOn`, `dischargeOn`, `CreatedOn`, `CreatedBy`) 
SELECT '1',OPDNo,RegNo,PatientId,PDep,'Kaya Chikitsa (Female)','1',pSymptom,'na',pname,PMob,uid,PSex, PAge,PMStatus,PFname,PAddr,PDist,PState,OPDDate,'0',OPDDate,'0000-00-00',OPDDate,'$user' 
from `opd` 
where OPDDate=STR_TO_DATE('$curDate','%Y-%m-%d 00:00:00') AND PDep='Kaya Chikitsa' AND PSex=0 GROUP BY `OPDNo`  
ORDER BY RAND() LIMIT $ipdKayaChikitsaF";

Above query is working properly:

Now kindly check select query mentioned in above query. Here I am selecting some values from table and some of them are hard codded. Now I want PBed value to be filled from a different table in which a condition should be there.

My bed table has 3 columns like

Ward                 |   Bed No | Status
Kaya Chikitsa (Female)       1        0
Kaya Chikitsa (Female)       2        0
Kaya Chikitsa (Female)       3        1
Kaya Chikitsa (Male)         4        0
Kaya Chikitsa (Male)         5        1
Kaya Chikitsa (Male)         6        0

I want to fill PBed with Bed No. from bed table and it should have condition of Ward='Kaya Chikitsa (Female)' and Status=0;

My result should be like this

Table ipdKayaChikitsaF

`IPDNo` | `OPDNo` | `RegNo` | `PatientId`  |   `PDep    |     `PWard`          | `PBed`  and so on...

1          267       1          12          Kaya Chikitsa  Kaya Chikitsa (Female)   1    and so on...
2          352       8          15          Kaya Chikitsa  Kaya Chikitsa (Female)   2    and so on...
and so on...

Kindly check above table her I want PBed column to be filled from bed table as conditions mentioned above.

Use join in select query like. Note table opd must have a join field of pward for this query to work and I have assumed that the bed table name is bed_table

INSERT INTO `ipdKayaChikitsaF`(`IPDNo`, `OPDNo`, `RegNo`, `PatientId`, `PDep`, `PWard`, `PBed`, `pSymptom`, `PDisease`, `PName`, `PMob`, `uid`, `PSex`, `PAge`, `PMStatus`, `PFname`, `PAddr`, `PDist`, `PState`, `OPDDate`, `dischargeStatus`, `admitOn`, `dischargeOn`, `CreatedOn`, `CreatedBy`) 
SELECT '1',o.OPDNo,o.RegNo,o.PatientId,o.PDep,'Kaya Chikitsa (Female)','1',o.pSymptom,'na',o.pname,o.PMob,o.uid,o.PSex,o. PAge,o.PMStatus,o.PFname,o.PAddr,o.PDist,o.PState,o.OPDDate,'0',b.Ward, b.`Bed No`,o.OPDDate,'$user' 
from `opd` as o join bed_table b on b.Ward = o.pward
where OPDDate=STR_TO_DATE('$curDate','%Y-%m-%d 00:00:00') AND PDep='Kaya Chikitsa' AND PSex=0 GROUP BY `OPDNo`  
ORDER BY RAND() LIMIT $ipdKayaChikitsaF" and b.Ward = 'Kaya Chikitsa (Female)' and b.status = '0';

As you have no relation field in opd table you can use simple cartesian product to get that exact value of ward Kaya Chikitsa (Female) with status 0. Also, In the above query previously forgot to replace the static ward and bed no value in select query ie 'Kaya Chikitsa (Female)','1' with b.Ward,b. Bed No . Therefore you were getting only value of "1" in bed no in the results.

    INSERT INTO `ipdKayaChikitsaF`(`IPDNo`, `OPDNo`, `RegNo`, `PatientId`, `PDep`, `PWard`, `PBed`, `pSymptom`, `PDisease`, `PName`, `PMob`, `uid`, `PSex`, `PAge`, `PMStatus`, `PFname`, `PAddr`, `PDist`, `PState`, `OPDDate`, `dischargeStatus`, `admitOn`, `dischargeOn`, `CreatedOn`, `CreatedBy`) 
    SELECT '1',o.OPDNo,o.RegNo,o.PatientId,o.PDep,b.Ward,b.`Bed No`,o.pSymptom,'na',o.pname,o.PMob,o.uid,o.PSex,o. PAge,o.PMStatus,o.PFname,o.PAddr,o.PDist,o.PState,o.OPDDate,'0',o.OPDDate,'0000-00-00',o.OPDDate,'$user' 
    from `opd` as o, (select Ward, `bed no` form bed_table where Status = 0 and Ward = 'Kaya Chikitsa (Female)' ) as b
    where OPDDate=STR_TO_DATE('$curDate','%Y-%m-%d 00:00:00') AND PDep='Kaya Chikitsa' AND PSex=0 GROUP BY `OPDNo`  
    ORDER BY RAND() LIMIT $ipdKayaChikitsaF" and b.Ward = 'Kaya Chikitsa (Female)' and b.status = '0';

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