CI中的多个MySQL查询

时间:2018-09-13 05:41:02

标签: php mysql sql codeigniter

我有一些查询,想在Codeigniter中执行。 下面是我尝试过的代码。

  function seememberfilter($course,$n)
        {


        $this->load->database();
        $sql = array();
        $sql[] = "CREATE TEMPORARY TABLE temp1";
        $sql[] = "select id,rand, name,lname, email,phone from `member` where course like ('%$course%')";
        $sql[] = "CREATE TEMPORARY table temp2";
        $sql[] = "select distinct t.id,t.rand, t.name,t.lname, t.email,t.phone, a.quest_id from temp1 t left join assignment a on t.rand= a.rand";
        $sql[] = "CREATE TEMPORARY table randid";
        $sql[] = "select rand, quest_id from temp2 where quest_id=$n";
        $sql[] = "select t.id,t.rand, t.name,t.lname, t.email,t.phone, r.quest_id from temp1 t left join randid r on t.rand= r.rand";

         foreach ($sql as $sql_command)
            {    
                if ($debugging == 0)
                {
                    $this->db->query($sql_command);
                } 
                elseif ($debugging == 1)
                {
                    echo $sql_command;
                } 
            } 
return $query->result();

        }

上面的代码在模型中,下面的代码在控制器中

 public function assign(){
            $this->load->model("user_model");
            $course=$this->input->post('course');
            $n=$this->input->post('id');
            $data['sel'] = $this->user_model->seememberfilter($course,$n);  
            $this->load->view('assign',$data);
        }

mysql查询在phpmyadmin中执行良好。我检查了

我想要的东西:我想在CI中成功执行mysql查询。

我正在对此进行编辑,并为您提供sql表的完整详细信息

DROP TABLE IF EXISTS `member`;
CREATE TABLE IF NOT EXISTS `member` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rand` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `lname` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `dob` varchar(255) NOT NULL,
  `course` text NOT NULL,
  `gender` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `city` varchar(255) NOT NULL,
  `state` varchar(255) NOT NULL,
  `zip` varchar(255) NOT NULL,
  `comment` text NOT NULL,
  `Aboutme` text NOT NULL,
  `dat` varchar(255) NOT NULL,
  `tim` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;



INSERT INTO `member` (`id`, `rand`, `email`, `name`, `lname`, `phone`, `dob`, `course`, `gender`, `address`, `city`, `state`, `zip`, `comment`, `Aboutme`, `dat`, `tim`) VALUES
(1, 'jrf20180961828', 'kk122344545@hotmail.com', 'Kishan', 'Yadav', '9717111111', '2018-09-01', 'paper first,Political Science,Philosophy,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'Good person.', '', '06-09-2018', '11:24:16pm'),
(2, 'jrf20180914721', 'kk1@gmail.com', 'Rohan', 'Yadav', '9717111111', '2018-09-01', 'paper first,Philosophy,History,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'Good Person.', '', '06-09-2018', '11:25:44pm'),
(5, 'jrf20180958284', 'ykishan94612121@gmail.com', 'kiran', 'Singh', '9717111111', '2018-07-07', 'paper first,Political Science,History,', 'female', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'nnbmnm', '', '10-09-2018', '10:11:34pm'),
(4, 'jrf20180932851', 'kk12@gmail.com', 'Pankaj', 'Yadav', '9717111111', '2018-09-01', 'paper first,Philosophy,Psychology,History,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'nmbmbn', '', '10-09-2018', '10:10:19pm'),
(6, 'jrf20180929250', 'hjh@gmail.com', 'John', 'Corter', '9717111111', '2018-09-06', 'paper first,History,', 'male', 'Diamond Auto Mobiles', 'Belthara road', 'ballia', '221715', 'sxs', '', '11-09-2018', '12:09:49am');
COMMIT;

第二张桌子是

DROP TABLE IF EXISTS `assignment`;
CREATE TABLE IF NOT EXISTS `assignment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rand` varchar(255) NOT NULL,
  `course` varchar(255) NOT NULL,
  `quest_id` varchar(255) NOT NULL,
  `time` varchar(255) NOT NULL,
  `date` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;



INSERT INTO `assignment` (`id`, `rand`, `course`, `quest_id`, `time`, `date`) VALUES
(1, 'jrf20180914721', 'paper first', '2', '10:05:48pm', '2018-09-10'),
(2, 'jrf20180961828', 'paper first', '2', '10:06:49pm', '10-09-2018'),
(3, 'jrf20180914721', 'History', '2', '10:08:03pm', '10-09-2018'),
(4, 'jrf20180958284', 'History', '2', '10:12:05pm', '10-09-2018'),
(5, 'jrf20180914721', 'paper first', '1', '10:19:23pm', '10-09-2018'),
(6, 'jrf20180932851', 'History', '3', '12:07:48am', '11-09-2018');
COMMIT;

我的输出应该是这样的,我加入两个表以获取此输出。 Attached pic of my output

我使用上面的查询得到了这个输出。对不起,我的英语。

1 个答案:

答案 0 :(得分:0)

在db-fiddle中聊天之后:

select distinct t.id, t.rand, name, lname, email, phone, if(quest_id='2','2',NULL) as quest-id
from `member` t
left join assignment a on t.rand= a.rand
where t.course like '%History%'
    and (a.course like '%History%') or a.id is null

限于课程的会员,指定相同课程的会员,如果找到quest(ion)_id 2,则显示该会员,否则将其保留为NULL。