mardi 14 juin 2016

How to join with unknown condition like pivot in mysql

I have the following code to

public function get_posted_questions($data) {

    include dirname(__FILE__) . "/database.php";
    $user_db_name = $dbconfig[$data['college_id']]['database'];

    if (isset($data['start'])) {
        $start = $data['start'];
    } else {
        $start = 0;
    }
    if (isset($data['end']) and ! empty($data['end'])) {
        $end = $data['end'];
    } else {
        $end = 30;
    }

    /*
     * multiple college database funda here goes
     */
    $max_college_id = "select distinct college_id from just_ask_question where status = '1' and isDeleted = '0' and college_id !='0' order by id desc limit $start,$end";
    $max_college_id_run = mysql_query($max_college_id);




    $question_data = array();
    $question_query = "select Q.id,Q.title,Q.description,Q.user_id,Q.college_id,Q.datetime,ifnull(group_concat(distinct T.name),'') as tags,ifnull(CONCAT_WS(' ',U.firstName,U.lastName),'') as user_name,ifnull(U.image,'') as image,ifnull(V.id,'') as no_of_view,ifnull(Vote.upvote,'') as up_vote,
                      ifnull(answer.id,0) as  no_of_answer,
                      ifnull(is_upvote,0) as upvote_status,category_name from 
                      just_ask_question as Q 
               left join just_ask_question_tag Qt on Qt.question_id = Q.id 
               left join just_ask_tag T on T.id = Qt.tag_id 
               left join just_ask_category on just_ask_category.id = Q.category_id
               left join (select count(id) as id,question_id from just_ask_answer) as answer on answer.question_id = Q.id";

    while ($row = mysql_fetch_assoc($max_college_id_run)){
          $user_db_name = $dbconfig[$row['college_id']]['database'];
        $question_query .= " case when Q.college_id = '".$row['college_id']."' then left join $user_db_name.users U on U.id = Q.user_id";

    }

        $question_query .= "  left join (select count(id) as id,question_id  from just_ask_view ) as V on V.question_id = Q.id
               left join (select count(upvote) as upvote,question_id from just_ask_upvote_downvote where upvote = '1' and is_question = '1' group by question_id) as Vote on Vote.question_id = Q.id 
               left join (select if(count(id) > 0,1,0) as is_upvote,question_id from just_ask_upvote_downvote where 
               upvote = '1' and college_id = '" . $data['college_id'] . "' and user_id = '" . $data['user_id'] . "' and is_question = '1') as Is_Vote on Is_Vote.question_id = Q.id where Q.status = '1' and Q.isDeleted = '0' and case when Q.visibility = 0 and Q.college_id != 0  then Q.college_id = '" . $data['college_id'] . "' else true end";




    if (!empty($data['search_text'])) {
        $search_text = $data['search_text'];
        $question_query .= " and (Q.title like '%$search_text%' or Q.description like '%$search_text%' or T.name like '%$search_text%')";
    }

    $question_query .= " group by Q.id order by Q.id desc limit $start,$end";

    $question_query_run = mysql_query($question_query);

    /* get weather question exist */
    $check_num_rows = mysql_num_rows($question_query_run);
    if ($check_num_rows > 0) {
        while ($row = mysql_fetch_assoc($question_query_run)) {
            if ($row['image'] != '') {
                $row['thumbnail'] = USER_THUMBNAIL_URL . $row['image'];
                $row['image'] = IMAGE_URL . $row['image'];
            } else
                $row['thumbnail'] = '';
            $question_data[] = $row;
        }
        $status['statuscode'] = "1";
        $status['statusmessage'] = "ok";
        $status['question_data'] = $question_data;
        $response['response'] = $status;
        echo json_encode($response);
        die();
    }else {
        $status['statuscode'] = "2";
        $status['statusmessage'] = "There is no record found";
        $response['response'] = $status;
        echo json_encode($response);
        die();
    }
}

AND the db config file like which contain the database name information like

    $dbconfig['1'] = array(
        'host' => 'localhost',
        'user_name' => 'root',
        'password' => 'test123*',
        'database' => 'staging_myuniversity'
    );


$dbconfig['2'] = array(
        'host' => 'localhost',
        'user_name' => 'root',
        'password' => 'test123*',
        'database' => 'staging_myuniversity_dias'
);

the database are reside on same server and with full permission.

the query generate like this

select Q.id,Q.title,Q.description,Q.user_id,Q.college_id,Q.datetime,ifnull(group_concat(distinct T.name),'') as tags,ifnull(CONCAT_WS(' ',U.firstName,U.lastName),'') as user_name,ifnull(U.image,'') as image,ifnull(V.id,'') as no_of_view,ifnull(Vote.upvote,'') as up_vote,
                          ifnull(answer.id,0) as  no_of_answer,
                          ifnull(is_upvote,0) as upvote_status,category_name from 
                          just_ask_question as Q 
                   left join just_ask_question_tag Qt on Qt.question_id = Q.id 
                   left join just_ask_tag T on T.id = Qt.tag_id 
                   left join just_ask_category on just_ask_category.id = Q.category_id
                   left join (select count(id) as id,question_id from just_ask_answer) as answer on answer.question_id = Q.id 
case when Q.college_id = '1' then left join staging_myuniversity.users U on U.id = Q.user_id  
case when Q.college_id = '12'then left join campify_solutions_mathura.users U on U.id = Q.user_id 
case when Q.college_id = '4' then left join staging_myuniversity_nit_kkr.users U on U.id = Q.user_id  
case when Q.college_id = '2' then left join staging_myuniversity_dias.users U on U.id = Q.user_id 

left join (select count(id) as id,question_id  from just_ask_view ) as V on V.question_id = Q.id
                   left join (select count(upvote) as upvote,question_id from just_ask_upvote_downvote where upvote = '1' and is_question = '1' group by question_id) as Vote on Vote.question_id = Q.id 
                   left join (select if(count(id) > 0,1,0) as is_upvote,question_id from just_ask_upvote_downvote where 
                   upvote = '1' and college_id = '1' and user_id = '1' and is_question = '1') as Is_Vote on Is_Vote.question_id = Q.id where Q.status = '1' and Q.isDeleted = '0' and case when Q.visibility = 0 and Q.college_id != 0  then Q.college_id = '1' else true end group by Q.id order by Q.id desc limit 0,30

I need to make the join on different databases based on college id first i fetch the distinct college id from the question then make a loop for college id and try to make join condition but i am getting error there is any suggestion and help

Aucun commentaire:

Enregistrer un commentaire