tp5写sql语句

白穹雨
  • 7

怎样用tp5写出如下sql?

SELECT * FROM `student` WHERE `sex` = 1 AND ((`age` IN (10,11,12) 

AND `name` LIKE '%李%') OR (`age` IN (15,16,17) AND `name` LIKE  '%长%'));
回复
阅读 512
2 个回答

大概应该是这样子。

Student::where('sex', '=', 1)
  ->where(function($query){
    $query->where(function($query){
      $query->where('age' , 'in', [10,11,12])->where('name','like','%李%');
    })
      ->orWhere(function($query){
        $query->where('age', 'in', [15,16,17])->where('name', 'like', '%长%');
      })
    
  })
$sql = "SELECT * FROM `student` WHERE `sex` = 1 AND ((`age` IN (10,11,12) AND `name` LIKE '%李%') OR (`age` IN (15,16,17) AND `name` LIKE  '%长%'));";
$list = Db::query($sql);
宣传栏