使用示例¶

insert¶

  1. // SqlMap
  2. 'insert' => [
  3. 'sql' => 'INSERT INTO market_goods #INSERT#',
  4. ]
  5.  
  6. //调用
  7. $data = [
  8. 'insert' => [
  9. 'market_id' => 1111,
  10. 'goods_id' => 2222
  11. ],
  12. ];
  13. $record = (yield Db::execute('dir_name.file_name.insert', $data));

batch insert¶

  1. // SqlMap
  2. 'batch_insert'=>[
  3. 'require' => [],
  4. 'limit' => [],
  5. 'sql' => 'INSERT INTO market_goods #INSERTS#',
  6. ]
  7. //调用
  8. $data = [
  9. 'inserts' => [
  10. [
  11. 'market_id' => 1111,
  12. 'goods_id' => 2222
  13. ],
  14. [
  15. 'market_id' => 222,
  16. 'goods_id' => 333
  17. ],
  18. ]
  19. ];
  20. $record = (yield Db::execute('dir_name.file_name.inserts', $data));

update¶

  1. // SqlMap
  2. 'update'=>[
  3. 'require' => ['market_id','goods_id'],
  4. 'limit' => [],
  5. 'sql' => 'UPDATE market_goods SET #DATA# WHERE market_id = #{market_id} AND goods_id = #{goods_id} LIMIT 1'
  6. ]
  7. //调用
  8. $data = [
  9. 'data' => [
  10. 'name' => 1111,
  11. 'time' => 2222
  12. ],
  13. 'var' => [
  14. 'market_id' => 222,
  15. 'goods_id' => 333
  16. ],
  17. ];
  18. $record = (yield Db::execute('dir_name.file_name.update', $data));

delete¶

  1. // SqlMap
  2. 'delete' => [
  3. 'require' => ['market_id','kdt_id','goods_id'],
  4. 'limit' => [],
  5. 'sql' => 'DELETE FROM market_goods WHERE market_id = #{market_id} AND kdt_id = #{kdt_id} AND goods_id = #{goods_id} LIMIT 1',
  6. ]
  7. //调用
  8. $data = [
  9. 'var' => [
  10. 'market_id' => 222,
  11. 'goods_id' => 333
  12. ],
  13. ];
  14. $record = (yield Db::execute('dir_name.file_name.delete', $data));

affected¶

  1. // SqlMap
  2. 'affected_update'=>[
  3. 'require' => ['market_id','goods_id'],
  4. 'limit' => [],
  5. 'sql' => 'UPDATE market_goods SET #DATA# WHERE market_id = #{market_id} AND goods_id = #{goods_id} LIMIT 1'
  6. ]
  7. //调用
  8. $data = [
  9. 'data' => [
  10. 'name' => 1111,
  11. 'time' => 2222
  12. ],
  13. 'var' => [
  14. 'market_id' => 222,
  15. 'goods_id' => 333
  16. ],
  17. ];
  18. $record = (yield Db::execute('dir_name.file_name.affected_update', $data));

row¶

  1. // SqlMap
  2. 'row_by_market_id_goods_id' => [
  3. 'sql' => 'SELECT * FROM market_goods WHERE market_id = #{market_id} AND goods_id = #{goods_id} LIMIT 1',
  4. ]
  5. //调用
  6. $data = [
  7. 'var' => [
  8. 'market_id' => 222,
  9. 'goods_id' => 333
  10. ],
  11. 'limit' => '0, 10'
  12. ];
  13. $record = (yield Db::execute('dir_name.file_name.row_by_market_id_goods_id', $data));

select¶

  1. // SqlMap
  2. 'select_by_market_id_goods_ids' => [
  3. 'require' => ['market_id','goods_id'],
  4. 'limit' => [],
  5. 'sql' => 'SELECT * FROM market_goods WHERE market_id = #{market_id} AND goods_id IN #{goods_id} #LIMIT#',
  6. ]
  7. //调用
  8. $data = [
  9. 'var' => [
  10. 'market_id' => 222,
  11. 'goods_id' => [333,111,333,555],
  12. ],
  13. 'limit' => '0, 10'
  14. ];
  15. $record = (yield Db::execute('dir_name.file_name.select_by_market_id_goods_ids', $data));

count¶

  1. // SqlMap
  2. 'count_by_market_id_audit_status'=>[
  3. 'require' => ['market_id','audit_status'],
  4. 'limit' => [],
  5. 'sql' => 'SELECT #COUNT# FROM market_goods WHERE market_id = #{market_id} AND audit_status = #{audit_status}',
  6. ]
  7. //调用
  8. $data = [
  9. 'count' => '*',
  10. 'var' => [
  11. 'market_id' => 222,
  12. 'audit_status' => 1
  13. ],
  14. ];
  15. $record = (yield Db::execute('dir_name.file_name.count_by_market_id_audit_status', $data));

raw¶

  1. // SqlMap
  2. 'raw_by_market_id_goods_ids' => [
  3. 'require' => ['market_id','goods_id'],
  4. 'limit' => [],
  5. 'sql' => 'SELECT * FROM market_goods WHERE market_id = #{market_id} AND goods_id IN #{goods_id} #LIMIT#',
  6. ]
  7. //调用
  8. $data = [
  9. 'var' => [
  10. 'market_id' => 222,
  11. 'goods_id' => [333,111,333,555],
  12. ],
  13. 'limit' => '0, 10'
  14. ];
  15. $record = (yield Db::execute('dir_name.file_name.raw_by_market_id_goods_ids', $data));

SqlMap 其他标签使用方法¶

order by¶

  1. //使用#ORDER#标签
  2. // SqlMap
  3. 'raw_by_market_id_goods_ids' => [
  4. 'require' => ['market_id','goods_id'],
  5. 'limit' => [],
  6. 'sql' => 'SELECT * FROM market_goods WHERE market_id = #{market_id} AND goods_id IN #{goods_id} #ORDER# #LIMIT#',
  7. ]
  8. //调用
  9. $data = [
  10. 'var' => [
  11. 'market_id' => 222,
  12. 'goods_id' => [333,111,333,555],
  13. ],
  14. 'order' => 'market_id DESC',
  15. 'limit' => '0, 10'
  16. ];
  17. $record = (yield Db::execute('dir_name.file_name.raw_by_market_id_goods_ids', $data));

group by¶

  1. // SqlMap
  2. 'group_by_market_id' => [
  3. 'require' => ['market_id','goods_id'],
  4. 'limit' => [],
  5. 'sql' => 'SELECT * FROM market_goods WHERE market_id = #{market_id} AND goods_id IN #{goods_id} #GROUP# #LIMIT#',
  6. ]
  7. //调用
  8. $data = [
  9. 'var' => [
  10. 'market_id' => 222,
  11. 'goods_id' => [333,111,333,555],
  12. ],
  13. 'group' => 'market_id',
  14. 'limit' => '0, 10'
  15. ];
  16. $record = (yield Db::execute('dir_name.file_name.group_by_market_id', $data));

原文: http://zanphpdoc.zanphp.io/libs/db/use_case.html