duanhe7471 2019-02-08 11:30
浏览 12

有没有办法结合mysqli查询或给他们一个优化序列?

I wonder if it is possible to optimize my queries to avoid having to send ten requests, to check if three main tables and two composite tables has the needed elements and if not then insert them.

("Composite table" is what i call a table that holds two primary keys that both are a reference to the primary key in another table, cause i do not know the correct term)

So i have tried to see if i can get all the needed ids from the three main tables in one query, but i can not it it to work.

Here is some of the things i have tried.

select t1.a AS EID, t2.a AS CID, t3.a AS IID from (select ID as a from Events where event = "test3") as t1, (select ID as a from Coordinates where Coordinates = "10.22,14.15") as t2, (select ID as a from Intervals where Interval = "From 12:15 To 18:30") as t3;

SELECT ID AS eid FROM Events WHERE event = "test3" UNION ALL SELECT ID AS cid FROM Coordinates WHERE Coordinates = "10.22,14.15" UNION ALL SELECT ID AS iid FROM Intervals WHERE Interval = "From 12:15 To 18:30"

And this is what i am currently doing

if(!empty($this->event) && $this->event!== false){
        $sql = "SELECT ID FROM Events WHERE event = ?";
        $values = array($this->event);
    $db->setQuery($sql, $values);
    $db->singleFetch();

    if($db->getResults() == 1){
        $eid = $db->getFetch()[0]['ID'];
    }
    else{
        $sql = "INSERT INTO Events (event, Description) VALUES (?, ?)";
        $values = array($this->event, $this->description);
        $db->setQuery($sql, $values);
        $db->singleQuery();
        $eid = $db->getLast();
    }
}
//I am doing this for all three main tables,
//to get the ids needed for me to be able to do this.

if(isset($eid) && isset($cid)){
    $sql = "SELECT count(*) FROM EC WHERE EID = ? AND CID = ?";
    $values = array($eid, $cid);
    $db->setQuery($sql, $values);
    $db->singleFetch();
    if($db->getFetch()[0]['count(*)'] == 0){
        $sql = "INSERT INTO EC (EID, CID) VALUES (?, ?)";
        $values = array($eid, $cid);
        $db->setQuery($sql, $values);
        $db->singleQuery();
    }
}
//Which i then do for both composite tables

Either i get my fetch array with every possible combination or i do not even get my fetch array and other times i get it but with two of the same event id and nothing else.

  • 写回答

1条回答 默认 最新

  • douzepao0281 2019-02-08 12:40
    关注

    You can write the stored procedure in MySQL for this and then you need to call that stored procedure

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)