开发者

Fetch the desired result from below given DB table structure of MySQL

开发者 https://www.devze.com 2023-01-31 23:45 出处:网络
below are the table structures according to which I have to develop the desired output(given at the end)

below are the table structures according to which I have to develop the desired output(given at the end)

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

Above are three tables from which I have to display the desired output as

Name : testing     
Company Name : Tesitng     

such that at first step I have doc_id then I get docatrtype_id and then docatrtypetype_id acording to these values i have to fetch the result. Also the query must see the doactrtypetype_id from table tbl_docatrtypetype and fetch the result from tbl_docatr from respective column docatr_float, docatr_int, docatr_date, docatr_varchar, docatr_blob

UPDATE

I Have created the below PHP functions for the queries please help me to get the easy one

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 as per @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;


For the sake of "integrity", you should add a (5, 'Blob') row to 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;

EDIT: Added doc_id specific values per OP comment.

0

精彩评论

暂无评论...
验证码 换一张
取 消