从下面给出的MySQL数据库表结构中获取所需的结果

时间:2010-12-22 12:36:22

标签: sql mysql

下面是表结构,我必须根据这些表结构开发所需的输出(最后给出)

tbl_docatr

docatr_id    doc_id     docatrtype_id   docatr_float    docatr_int  docatr_date     docatr_varchar  docatr_blob
    1       12            1              NULL            NULL        NULL            testing       [BLOB - NULL]
    2       12            2              NULL            NULL        NULL            Tesitng       [BLOB - NULL]

tbl_docatrtype

docatrtype_id   docatrtypegroup_id  docatrtypetype_id   docatrtype_name     
    1                      1                 4               Name    
    2                      1                 4             Company Name      

tbl_docatrtypetype

docatrtypetype_id   docatrtypetype_name
         1                Float
         2                Int
         3                Date
          4                String line

上面是三个表,我必须从中显示所需的输出

Name : testing     
Company Name : Tesitng     

这样,在第一步我有doc_id然后我得到docatrtype_id然后docatrtypetype_id根据这些值我必须获取结果。此外,查询必须从表tbl_docatrtypetype中查看doactrtypetype_id,并从各自的列docatr_float,docatr_int,docatr_date,docatr_varchar,docatr_blob

中获取tbl_docatr的结果。
  

更新

我为查询创建了以下PHP函数,请帮助我轻松搞定

function getDocumentDetail($doc_id){

        $arr_document_detail = array();

        $query = "SELECT * FROM tbl_doc WHERE doc_id = '".$doc_id."'";

        $this->connect->executeQuery($query, $this->connect->conn);
        if($this->connect->numRows() > 0) {
            while($row = $this->connect->getRowArr()){              
                    $arr_document_detail = $row;

            }
        }
        return $arr_document_detail;

    }

    //Getting Attribute Details
    function getAttributeDetails($doc_id){
        $arr_attrtype_id = array();
        $query = "SELECT
                    docatrtype_id,
                    docatr_float,
                    docatr_int,
                    docatr_varchar,
                    docatr_date,
                    docatr_blob
                FROM
                    tbl_docatr
                WHERE
                    doc_id = '".$doc_id."'";
    $this->connect->executeQuery($query, $this->connect->conn);
    if($this->connect->numRows() > 0){
    $j = 0;
            while($row1 = $this->connect->getRowArr()){
                $arr_attrtype_id[$j] =$row1;
                $j++;                   
                }
            }
        return $arr_attrtype_id;
    }


    function getAttrTypetype($attrtype_id){

        $arr_attrtypetype = array();    
        $query = "SELECT
                    docatrtype_name,
                    docatrtype_id,
                    docatrtypegroup_id,
                    docatrtypetype_id

                FROM
                    tbl_docatrtype
                WHERE
                    docatrtype_id = '".$attrtype_id."'";
    $this->connect->executeQuery($query, $this->connect->conn);
    if($this->connect->numRows() > 0){
    $i = 0;
            while($row1 = $this->connect->getRowArr()){
                $arr_attrtypetype[$i] =$row1;
                $i++;                   
                }
            }
            return $arr_attrtypetype;

        }

    function getAttrtypetypedetail($attrtypetype_id){

        $arr_attrtypetype_detail = array(); 
        $query = "SELECT
                    docatrtypetype_name,
                    docatrtypetype_id
                FROM
                    tbl_docatrtypetype
                WHERE
                    docatrtypetype_id = '".$attrtypetype_id."'";
    $this->connect->executeQuery($query, $this->connect->conn);
    if($this->connect->numRows() > 0){
    $i = 0;
            while($row1 = $this->connect->getRowArr()){
                $arr_attrtypetype_detail[$i] =$row1;
                $i++;                   
                }
            }
            return $arr_attrtypetype_detail;

        }
  

UPDATE - 2 @ @Danosaure

select concat(dat.docatrtype_name, ':',
    case dat.docatrtypetype_id
        when '1' then da.docatr_float
        when '2' then da.docatr_int
        when '3' then da.docatr_date
        when '4' then da.docatr_varchar
        when '5' then da.docatr_blob
     end)  as 'Value'
from tbl_docatr da
    inner join tbl_docatrtype dat using (docatrtype_id)
    inner join tbl_docatrtypetype datt using (docatrtypetype_id)
WHERE da.doc_id=33
ORDER BY da.docatr_id;

1 个答案:

答案 0 :(得分:1)

为了“完整性”,您应该向(5, 'Blob')添加tbl_docatrtypetype行。

select dat.docatrtype_name,
    case datt.docatrtypetype_name
        when 'Float' then da.docatr_float
        when 'Int' then da.docatr_int
        when 'Date' then da.docatr_date
        when 'String line' then da.docatr_varchar
        else da.docatr_blob
     end  as 'Value'
from tbl_docatr da
    inner join tbl_docatrtype dat using (docatrtype_id)
    inner join tbl_docatrtypetype datt using (docatrtypetype_id)
WHERE da.doc_id=12
ORDER BY da.docatr_id;

编辑:为每个OP评论添加了doc_id特定值。