简体   繁体   English

查询从2个表中选择多个值

[英]Query for select multiple values from 2 tables

Table names: 表名:

  1. tbla_can_types

Fields are fac_id,type_id,type_name,status 字段是fac_id,type_id,type_name,status

  1. tbla_canteen_rates (there is no data in this table) tbla_canteen_rates (此表中没有数据)

Fields are `fac_id,cat_id,type_id,from time,to_time,rate,off_rate,status,effective_date.,can_id(sequence number) 字段是`fac_id,cat_id,type_id,从时间,到_时间,速率,脱离速率,状态,生效日期。,can_id(序列号)

<?php
include('adodb/adodb.inc.php');
$conn=&ADONewconnection('oci8');
$conn->Pconnect('conn','hostname','username','pwd');
$fac_id=$_GET['fac_id'];
$cat_id=$_GET['cat_id'];
$file=fopen("text.txt","w+");
global $newId;
header("Content-type: text/xml");
echo('<?xml version="1.0" encoding="iso-8859-1"?>');
echo "<data>";
$ids = explode(",",$_POST["ids"]);

$rates="select type_id,from_time,to_time,rate,off_rate,status,to_char(effective_date,'dd-Mon-yyyy') e_date,can_id from tbla_canteen_rates where fac_id=$fac_id and cat_id=$cat_id order by to_number(substr(from_time,0,length(from_time)-3))";
    $rs1=$conn->Execute($rates);
    if(!$rs1->EOF)
    echo $rs1->fields[0].'~'.$rs1->fields[1].'~'.$rs1->fields[2].'~'.$rs1->fields[3].'~'.$rs1->fields[4].'~'.$rs1->fields[5].'~'.$rs1->fields[6].'~'.$rs1->fields[7];
    $temp_id=$rs1->fields[0];
    echo $temp_id;
    $rs1->MoveNext();

    for ($i=0; $i < sizeof($ids); $i++)
    { 
        $rowId = $ids[$i]; //id or row which was updated 
        $newId = $rowId; //will be used for insert operation    
        $mode = $_POST[$rowId."_!nativeeditor_status"]; //get request mode
        switch($mode)
        {
        case "inserted":
          $insert= "insert into tbla_canteen_rates(type_id,from_time,to_time,rate,off_rate,status,effective_date,fac_id,cat_id )values
            ($temp_id,
             '".$_POST[$rowId."_c1"]."',
             '".$_POST[$rowId."_c2"]."',
             '".$_POST[$rowId."_c3"]."',
             '".$_POST[$rowId."_c4"]."',
             '".$_POST[$rowId."_c5"]."',to_date('".$_POST[$rowId."_c6"]."','dd-Mon-yyyy'),$fac_id,$cat_id)";
          $conn->Execute($insert);
          fwrite($file,$insert);
          $action='add_row($rowId)';
        break;
        case "deleted":
            $delete = "delete from tbla_canteen_rates where can_id=".$rowId ;
            $conn->Execute($delete);
            fwrite($file,$delete);
            $action ='delete_row($rowId)';
        break;
        default:
            $update ="update tbla_canteen_rates set 
                    type_id=$temp_id,
                    from_time='".$_POST[$rowId."_c1"]."',
                    to_time='".$_POST[$rowId."_c2"]."',
                    rate='".$_POST[$rowId."_c3"]."',
                    off_rate='".$_POST[$rowId."_c4"]."',
                    status='".$_POST[$rowId."_c5"]."',
                    effective_date=to_date('".$_POST[$rowId."_c6"]."','dd-Mon-yyyy'),
                    fac_id='$fac_id',cat_id='$cat_id'
                    where  can_id=".$rowId;
            $conn->Execute($update);
            fwrite($file,$update);
            $action = 'update_row($rowId)';
        break;
        }   
        echo "<action type='".$action."' sid='".$rowId."' tid='".$newId."'/>";
    }
    echo "</data>";
    ?>  

In the query i need id for type_id in a variable i have written but every time it is getting id of first element only. 在查询中,我需要在已编写的变量中输入type_id的id,但是每次仅获取第一个元素的id时。

in this i need an id for type_id. 在此,我需要一个type_id的ID。

please send me the code 4 that 请给我发送代码4

Regards 问候

Pawan 帕万

I've got a sneaking suspicion that you've got several questions in there, but since you asked about the sql query, this should work: 我暗中怀疑您那里有几个问题,但是由于您询问了sql查询,因此应该可以:

Note: I'm assuming fac_id is the id that binds the two tables together. 注意:我假设fac_id是将两个表绑定在一起的ID。 "var_fac_id" and "var_cat_id" are the values from your comboboxes. “ var_fac_id”和“ var_cat_id”是组合框中的值。

Select t1.type_name, t2.from_time, t2.to_time, t2.rate
FROM tbla_can_types as t1 LEFT JOIN tbla_canteen_rates as t2
ON t1.fac_id = t2.fac_id
WHERE t1.fac_id = var_fac_id AND t2.cat_id = var_cat_id

Further notes: I think this should work despite the "AND t2.cat_id = var_cat_id" (which obviously would find not results in an empty table), since it's a left join. 进一步说明:我认为尽管“ AND t2.cat_id = var_cat_id”(显然不会导致空表)仍然有效,因为它是左连接。 If it returns an empty set, however, see if it works if you remove it. 但是,如果它返回一个空集,请查看它是否可以删除。

Update: 更新:
your comment above makes me think the tables are bound together by type_id instead. 您在上面的评论使我认为这些表由type_id绑定在一起。 If that's the case, use ON t1.type_id = t2.type_id instead of ON t1.fac_id = t2.fac_id 如果是这种情况,请使用ON t1.type_id = t2.type_id代替ON t1.fac_id = t2.fac_id

Update #2 based on poster's comments 根据发布者的评论更新#2
I'm not going to do your homework, dude. 老兄,我不会做功课。 And homework is the only place where they'd tell you to not do joins. 作业是他们告诉您不要参加的唯一场所。 I will send you to some resources that will help you learn this stuff, though. 不过,我将向您发送一些资源,以帮助您学习这些知识。 Check out the basic mysql example from php.net: http://www.php.net/manual/en/mysql.examples-basic.php . 从php.net查看基本的mysql示例: http ://www.php.net/manual/en/mysql.examples-basic.php。 That sample actually gets you pretty close to what you need. 该样本实际上使您非常接近所需。 Also their overall mysql documentation, which is good for looking up what specific functions do: http://www.php.net/manual/en/book.mysql.php . 还有它们的总体mysql文档,这对于查找特定功能有帮助: http : //www.php.net/manual/zh/book.mysql.php Finally, remember that you're learning two languages here -- PHP, along with its functions to access a mysql DB, and -- SQL, which is a query language used within DBs. 最后,请记住,您在这里正在学习两种语言-PHP及其访问mysql数据库的功能,以及-SQL,这是DB中使用的一种查询语言。 For SQL, check out http://sqlzoo.net/ , which seems a decent introduction on how to write queries. 对于SQL,请访问http://sqlzoo.net/ ,这似乎是有关如何编写查询的不错的介绍。

Good luck on your assignment. 祝您工作顺利。

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

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