查询语言.where

Testing Is Documentation

tests/Database/Query/WhereTest.php查询语言.where - 图1

Uses

  1. <?php
  2. use Leevel\Database\Condition;
  3. use Tests\Database\DatabaseTestCase as TestCase;

where 查询条件

最基本的用法为字段 (表达式) 值。

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. // 字段 (表达式) 值
  5. $sql = <<<'eot'
  6. [
  7. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id",
  8. {
  9. "test_query_id": [
  10. 1
  11. ]
  12. },
  13. false
  14. ]
  15. eot;
  16. $this->assertSame(
  17. $sql,
  18. $this->varJson(
  19. $connect
  20. ->table('test_query')
  21. ->where('id', '=', 1)
  22. ->findAll(true)
  23. )
  24. );
  25. }

where 查询条件默认为等于 =

  1. public function testBaseUse2(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id",
  7. {
  8. "test_query_id": [
  9. 2
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->where('id', 2)
  21. ->findAll(true),
  22. 1
  23. )
  24. );
  25. }

where 查询条件支持多次调用

  1. public function testBaseUse3(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id AND `test_query`.`name` > :test_query_name AND `test_query`.`value` LIKE :test_query_value",
  7. {
  8. "test_query_id": [
  9. 2
  10. ],
  11. "test_query_name": [
  12. "狗蛋"
  13. ],
  14. "test_query_value": [
  15. "小鸭子"
  16. ]
  17. },
  18. false
  19. ]
  20. eot;
  21. $this->assertSame(
  22. $sql,
  23. $this->varJson(
  24. $connect
  25. ->table('test_query')
  26. ->where('id', 2)
  27. ->where('name', '>', '狗蛋')
  28. ->where('value', 'like', '小鸭子')
  29. ->findAll(true),
  30. 2
  31. )
  32. );
  33. }

where 查询条件支持数组方式

  1. public function testArray(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`name` LIKE :test_query_name",
  7. {
  8. "test_query_name": [
  9. "技术"
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->where(['name', 'like', '技术'])
  21. ->findAll(true)
  22. )
  23. );
  24. }

where 查询条件支持二维数组多个条件

  1. public function testMultiDimensionalArray(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`name` LIKE :test_query_name AND `test_query`.`value` <> :test_query_value",
  7. {
  8. "test_query_name": [
  9. "技术"
  10. ],
  11. "test_query_value": [
  12. "结局"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->where([
  24. ['name', 'like', '技术'],
  25. ['value', '<>', '结局'],
  26. ])
  27. ->findAll(true),
  28. 1
  29. )
  30. );
  31. }

orWhere 查询条件

  1. public function testOrWhere(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`name` LIKE :test_query_name OR `test_query`.`value` <> :test_query_value",
  7. {
  8. "test_query_name": [
  9. "技术"
  10. ],
  11. "test_query_value": [
  12. "结局"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->where('name', 'like', '技术')
  24. ->orWhere('value', '<>', '结局')
  25. ->findAll(true)
  26. )
  27. );
  28. }

whereBetween 查询条件

  1. public function testWhereBetween(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` BETWEEN :test_query_id_between0 AND :test_query_id_between1",
  7. {
  8. "test_query_id_between0": [
  9. 1
  10. ],
  11. "test_query_id_between1": [
  12. 100
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->whereBetween('id', [1, 100])
  24. ->findAll(true)
  25. )
  26. );
  27. $sql = <<<'eot'
  28. [
  29. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` BETWEEN :test_query_id_between0 AND :test_query_id_between1",
  30. {
  31. "test_query_id_between0": [
  32. 1
  33. ],
  34. "test_query_id_between1": [
  35. 10
  36. ]
  37. },
  38. false
  39. ]
  40. eot;
  41. $this->assertSame(
  42. $sql,
  43. $this->varJson(
  44. $connect
  45. ->table('test_query')
  46. ->where('id', 'between', [1, 10])
  47. ->findAll(true),
  48. 1
  49. )
  50. );
  51. $sql = <<<'eot'
  52. [
  53. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` BETWEEN :test_query_id_between0 AND :test_query_id_between1 AND `test_query`.`name` BETWEEN :test_query_name_between0 AND :test_query_name_between1",
  54. {
  55. "test_query_id_between0": [
  56. 1
  57. ],
  58. "test_query_id_between1": [
  59. 100
  60. ],
  61. "test_query_name_between0": [
  62. 5
  63. ],
  64. "test_query_name_between1": [
  65. 22
  66. ]
  67. },
  68. false
  69. ]
  70. eot;
  71. $this->assertSame(
  72. $sql,
  73. $this->varJson(
  74. $connect
  75. ->table('test_query')
  76. ->whereBetween([
  77. ['id', [1, 100]],
  78. ['name', [5, 22]],
  79. ])
  80. ->findAll(true),
  81. 2
  82. )
  83. );
  84. }

whereNotBetween 查询条件

  1. public function testWhereNotBetween(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` NOT BETWEEN :test_query_id_notbetween0 AND :test_query_id_notbetween1",
  7. {
  8. "test_query_id_notbetween0": [
  9. 1
  10. ],
  11. "test_query_id_notbetween1": [
  12. 10
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->whereNotBetween('id', [1, 10])
  24. ->findAll(true)
  25. )
  26. );
  27. $sql = <<<'eot'
  28. [
  29. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` NOT BETWEEN :test_query_id_notbetween0 AND :test_query_id_notbetween1",
  30. {
  31. "test_query_id_notbetween0": [
  32. 1
  33. ],
  34. "test_query_id_notbetween1": [
  35. 10
  36. ]
  37. },
  38. false
  39. ]
  40. eot;
  41. $this->assertSame(
  42. $sql,
  43. $this->varJson(
  44. $connect
  45. ->table('test_query')
  46. ->where('id', 'not between', [1, 10])
  47. ->findAll(true),
  48. 1
  49. )
  50. );
  51. }

whereIn 查询条件

  1. public function testWhereIn(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IN (:test_query_id_in0,:test_query_id_in1)",
  7. {
  8. "test_query_id_in0": [
  9. 2
  10. ],
  11. "test_query_id_in1": [
  12. 50
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->whereIn('id', [2, 50])
  24. ->findAll(true)
  25. )
  26. );
  27. $sql = <<<'eot'
  28. [
  29. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IN (:test_query_id_in0,:test_query_id_in1)",
  30. {
  31. "test_query_id_in0": [
  32. "1"
  33. ],
  34. "test_query_id_in1": [
  35. "10"
  36. ]
  37. },
  38. false
  39. ]
  40. eot;
  41. $this->assertSame(
  42. $sql,
  43. $this->varJson(
  44. $connect
  45. ->table('test_query')
  46. ->where('id', 'in', '1,10')
  47. ->findAll(true),
  48. 1
  49. )
  50. );
  51. $sql = <<<'eot'
  52. [
  53. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IN (:test_query_id_in0,:test_query_id_in1)",
  54. {
  55. "test_query_id_in0": [
  56. 2
  57. ],
  58. "test_query_id_in1": [
  59. 50
  60. ]
  61. },
  62. false
  63. ]
  64. eot;
  65. $this->assertSame(
  66. $sql,
  67. $this->varJson(
  68. $connect
  69. ->table('test_query')
  70. ->where('id', 'in', [2, 50])
  71. ->findAll(true),
  72. 2
  73. )
  74. );
  75. }

whereNotIn 查询条件

  1. public function testWhereNotIn(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` NOT IN (:test_query_id_in0,:test_query_id_in1)",
  7. {
  8. "test_query_id_in0": [
  9. 2
  10. ],
  11. "test_query_id_in1": [
  12. 50
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->whereNotIn('id', [2, 50])
  24. ->findAll(true)
  25. )
  26. );
  27. $sql = <<<'eot'
  28. [
  29. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` NOT IN (:test_query_id_in0,:test_query_id_in1)",
  30. {
  31. "test_query_id_in0": [
  32. "1"
  33. ],
  34. "test_query_id_in1": [
  35. "10"
  36. ]
  37. },
  38. false
  39. ]
  40. eot;
  41. $this->assertSame(
  42. $sql,
  43. $this->varJson(
  44. $connect
  45. ->table('test_query')
  46. ->where('id', 'not in', '1,10')
  47. ->findAll(true),
  48. 1
  49. )
  50. );
  51. }

whereNull 查询条件

  1. public function testWhereNull(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IS NULL",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->whereNull('id')
  17. ->findAll(true)
  18. )
  19. );
  20. $this->assertSame(
  21. $sql,
  22. $this->varJson(
  23. $connect
  24. ->table('test_query')
  25. ->where('id', 'null')
  26. ->findAll(true),
  27. 1
  28. )
  29. );
  30. }

whereNotNull 查询条件

  1. public function testWhereNotNull(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IS NOT NULL",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->whereNotNull('id')
  17. ->findAll(true)
  18. )
  19. );
  20. $this->assertSame(
  21. $sql,
  22. $this->varJson(
  23. $connect
  24. ->table('test_query')
  25. ->where('id', 'not null')
  26. ->findAll(true),
  27. 1
  28. )
  29. );
  30. }

where 查询条件未指定值默认为 null

  1. public function testWhereDefaultNull(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IS NULL",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->where('id')
  17. ->findAll(true)
  18. )
  19. );
  20. }

where 查询条件指定值为 null

  1. public function testWhereEqualNull(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IS NULL",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->where('id', '=', null)
  17. ->findAll(true)
  18. )
  19. );
  20. }

whereLike 查询条件

  1. public function testWhereLike(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` LIKE :test_query_id",
  7. {
  8. "test_query_id": [
  9. "5"
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->whereLike('id', '5')
  21. ->findAll(true)
  22. )
  23. );
  24. $this->assertSame(
  25. $sql,
  26. $this->varJson(
  27. $connect
  28. ->table('test_query')
  29. ->where('id', 'like', '5')
  30. ->findAll(true),
  31. 1
  32. )
  33. );
  34. }

whereNotLike 查询条件

  1. public function testWhereNotLike(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` NOT LIKE :test_query_id",
  7. {
  8. "test_query_id": [
  9. "5"
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->whereNotLike('id', '5')
  21. ->findAll(true)
  22. )
  23. );
  24. $this->assertSame(
  25. $sql,
  26. $this->varJson(
  27. $connect
  28. ->table('test_query')
  29. ->where('id', 'not like', '5')
  30. ->findAll(true),
  31. 1
  32. )
  33. );
  34. }

whereExists 查询条件

  1. public function testWhereExists(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE EXISTS (SELECT `test_query_subsql`.* FROM `test_query_subsql` WHERE `test_query_subsql`.`id` = :test_query_exists_test_query_subsql_id)",
  7. {
  8. "test_query_exists_test_query_subsql_id": [
  9. 1
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->whereExists(
  21. function ($select) {
  22. $select->table('test_query_subsql')->where('id', 1);
  23. }
  24. )
  25. ->findAll(true)
  26. )
  27. );
  28. $sql = <<<'eot'
  29. [
  30. "SELECT `test_query`.* FROM `test_query` WHERE EXISTS (SELECT `test_query_subsql`.* FROM `test_query_subsql`)",
  31. [],
  32. false
  33. ]
  34. eot;
  35. $subSelect = $connect->table('test_query_subsql');
  36. $this->assertSame(
  37. $sql,
  38. $this->varJson(
  39. $connect
  40. ->table('test_query')
  41. ->where([':exists' => $subSelect])
  42. ->findAll(true),
  43. 1
  44. )
  45. );
  46. $sql = <<<'eot'
  47. [
  48. "SELECT `test_query`.* FROM `test_query` WHERE EXISTS (select *from test_query_subsql)",
  49. [],
  50. false
  51. ]
  52. eot;
  53. $this->assertSame(
  54. $sql,
  55. $this->varJson(
  56. $connect
  57. ->table('test_query')
  58. ->where([':exists' => 'select *from test_query_subsql'])
  59. ->findAll(true),
  60. 2
  61. )
  62. );
  63. $sql = <<<'eot'
  64. [
  65. "SELECT `test_query`.* FROM `test_query` WHERE EXISTS (SELECT `test_query_subsql`.* FROM `test_query_subsql` WHERE `test_query_subsql`.`id` = :test_query_exists_test_query_subsql_id)",
  66. {
  67. "test_query_exists_test_query_subsql_id": [
  68. 1
  69. ]
  70. },
  71. false
  72. ]
  73. eot;
  74. $this->assertSame(
  75. $sql,
  76. $this->varJson(
  77. $connect
  78. ->table('test_query')
  79. ->where(
  80. [
  81. ':exists' => function ($select) {
  82. $select
  83. ->table('test_query_subsql')
  84. ->where('id', 1);
  85. },
  86. ]
  87. )
  88. ->findAll(true),
  89. 3
  90. )
  91. );
  92. }

whereNotExists 查询条件

  1. public function testWhereNotExists(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE NOT EXISTS (SELECT `test_query_subsql`.* FROM `test_query_subsql` WHERE `test_query_subsql`.`id` = :test_query_notexists_test_query_subsql_id)",
  7. {
  8. "test_query_notexists_test_query_subsql_id": [
  9. 1
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->whereNotExists(
  21. function ($select) {
  22. $select
  23. ->table('test_query_subsql')
  24. ->where('id', 1);
  25. }
  26. )
  27. ->findAll(true)
  28. )
  29. );
  30. }

where 查询条件支持分组

  1. public function testWhereGroup(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id OR (`test_query`.`votes` > :test_query_votes AND `test_query`.`title` <> :test_query_title)",
  7. {
  8. "test_query_votes": [
  9. 100
  10. ],
  11. "test_query_title": [
  12. "Admin"
  13. ],
  14. "test_query_id": [
  15. 5
  16. ]
  17. },
  18. false
  19. ]
  20. eot;
  21. $this->assertSame(
  22. $sql,
  23. $this->varJson(
  24. $connect
  25. ->table('test_query')
  26. ->where('id', 5)
  27. ->orWhere(function ($select) {
  28. $select
  29. ->where('votes', '>', 100)
  30. ->where('title', '<>', 'Admin');
  31. })
  32. ->findAll(true)
  33. )
  34. );
  35. $sql = <<<'eot'
  36. [
  37. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id OR `test_query`.`name` = :test_query_name AND (`test_query`.`votes` > :test_query_votes OR `test_query`.`title` <> :test_query_title)",
  38. {
  39. "test_query_votes": [
  40. 100
  41. ],
  42. "test_query_title": [
  43. "Admin"
  44. ],
  45. "test_query_id": [
  46. 5
  47. ],
  48. "test_query_name": [
  49. "小牛"
  50. ]
  51. },
  52. false
  53. ]
  54. eot;
  55. $this->assertSame(
  56. $sql,
  57. $this->varJson(
  58. $connect
  59. ->table('test_query')
  60. ->where('id', 5)
  61. ->orWhere('name', '小牛')
  62. ->where(function ($select) {
  63. $select
  64. ->where('votes', '>', 100)
  65. ->orWhere('title', '<>', 'Admin');
  66. })
  67. ->findAll(true),
  68. 1
  69. )
  70. );
  71. }

where 查询条件支持表达式

  1. public function testConditionalExpression(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`post`,`test_query`.`value`,concat(\"tt_\",`test_query`.`id`) FROM `test_query` WHERE concat(\"hello_\",`test_query`.`posts`) = `test_query`.`id`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'post,value,'.Condition::raw('concat("tt_",[id])'))
  16. ->where(Condition::raw('concat("hello_",[posts])'), '=', Condition::raw('[id]'))
  17. ->findAll(true)
  18. )
  19. );
  20. }

where 查询条件支持二维数组的键值为字段

  1. public function testArrayKeyAsField(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id AND `test_query`.`name` IN (:test_query_name_in0,:test_query_name_in1,:test_query_name_in2) AND `test_query`.`weidao` BETWEEN :test_query_weidao_between0 AND :test_query_weidao_between1 AND `test_query`.`value` IS NULL AND `test_query`.`remark` IS NOT NULL AND `test_query`.`goods` = :test_query_goods AND `test_query`.`hello` = :test_query_hello",
  7. {
  8. "test_query_id": [
  9. "故事"
  10. ],
  11. "test_query_name_in0": [
  12. 1
  13. ],
  14. "test_query_name_in1": [
  15. 2
  16. ],
  17. "test_query_name_in2": [
  18. 3
  19. ],
  20. "test_query_weidao_between0": [
  21. "40"
  22. ],
  23. "test_query_weidao_between1": [
  24. "100"
  25. ],
  26. "test_query_goods": [
  27. "东亚商品"
  28. ],
  29. "test_query_hello": [
  30. "world"
  31. ]
  32. },
  33. false
  34. ]
  35. eot;
  36. $this->assertSame(
  37. $sql,
  38. $this->varJson(
  39. $connect
  40. ->table('test_query')
  41. ->where([
  42. 'id' => ['=', '故事'],
  43. 'name' => ['in', [1, 2, 3]],
  44. 'weidao' => ['between', '40,100'],
  45. 'value' => 'null',
  46. 'remark' => ['not null'],
  47. 'goods' => '东亚商品',
  48. 'hello' => ['world'],
  49. ])
  50. ->findAll(true)
  51. )
  52. );
  53. }

where 查询条件支持字符串语法 :string

  1. public function testSupportString(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`name` = 11 and `test_query`.`value` = 22 and concat(\"tt_\",`test_query`.`id`)",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->where([':string' => Condition::raw('[name] = 11 and [test_query.value] = 22 and concat("tt_",[id])')])
  17. ->findAll(true)
  18. )
  19. );
  20. }

where 查询条件支持分组语法 :subor 和 suband

  1. public function testSupportSubandSubor(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`hello` = :test_query_hello OR (`test_query`.`id` LIKE :test_query_subor_test_query_id)",
  7. {
  8. "test_query_subor_test_query_id": [
  9. "你好"
  10. ],
  11. "test_query_hello": [
  12. "world"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->where([
  24. 'hello' => 'world',
  25. ':subor' => ['id', 'like', '你好'],
  26. ])
  27. ->findAll(true)
  28. )
  29. );
  30. }

where 查询条件支持分组语法 :subor 和 suband 任意嵌套

  1. public function testSupportSubandSuborMore(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`hello` = :test_query_hello OR (`test_query`.`id` LIKE :test_query_subor_test_query_id AND `test_query`.`value` = :test_query_subor_test_query_value) AND (`test_query`.`id2` LIKE :test_query_suband_test_query_id2 OR `test_query`.`value2` = :test_query_suband_test_query_value2 OR (`test_query`.`child_one` > :test_query_subor_test_query_child_one AND `test_query`.`child_two` LIKE :test_query_subor_test_query_child_two))",
  7. {
  8. "test_query_subor_test_query_child_one": [
  9. "123"
  10. ],
  11. "test_query_subor_test_query_child_two": [
  12. "123"
  13. ],
  14. "test_query_suband_test_query_id2": [
  15. "你好2"
  16. ],
  17. "test_query_suband_test_query_value2": [
  18. "helloworld2"
  19. ],
  20. "test_query_subor_test_query_id": [
  21. "你好"
  22. ],
  23. "test_query_subor_test_query_value": [
  24. "helloworld"
  25. ],
  26. "test_query_hello": [
  27. "111"
  28. ]
  29. },
  30. false
  31. ]
  32. eot;
  33. $this->assertSame(
  34. $sql,
  35. $this->varJson(
  36. $connect
  37. ->table('test_query')
  38. ->where([
  39. 'hello' => '111',
  40. ':subor' => [
  41. ['id', 'like', '你好'],
  42. ['value', '=', 'helloworld'],
  43. ],
  44. ':suband' => [
  45. ':logic' => 'or',
  46. ['id2', 'like', '你好2'],
  47. ['value2', '=', 'helloworld2'],
  48. ':subor' => [
  49. ['child_one', '>', '123'],
  50. ['child_two', 'like', '123'],
  51. ],
  52. ],
  53. ])
  54. ->findAll(true),
  55. 1
  56. )
  57. );
  58. }

where 查询条件字段可以指定表

字段条件用法和 table 中的字段用法一致,详情可以查看《查询语言.table》。

  1. public function testWhereFieldWithTable(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`name` = :test_query_name",
  7. {
  8. "test_query_name": [
  9. 1
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->where('test_query.name', '=', 1)
  21. ->findAll(true)
  22. )
  23. );
  24. }

where 查询条件支持复杂的子查询

  1. public function testWhereInArrayItemIsClosure(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` IN ((SELECT `test_query_subsql`.`id` FROM `test_query_subsql` WHERE `test_query_subsql`.`id` = :test_query_id_test_query_subsql_id),:test_query_id_in1)",
  7. {
  8. "test_query_id_test_query_subsql_id": [
  9. 1
  10. ],
  11. "test_query_id_in1": [
  12. 100
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->table('test_query')
  23. ->whereIn('id', [function ($select) {
  24. $select
  25. ->table('test_query_subsql', 'id')
  26. ->where('id', 1);
  27. }, 100])
  28. ->findAll(true)
  29. )
  30. );
  31. }

whereRaw 查询条件

  1. public function testWhereRaw(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE FIND_IN_SET(1, id)",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->whereRaw('FIND_IN_SET(1, id)')
  17. ->findAll(true)
  18. )
  19. );
  20. }

orWhereRaw 查询条件

  1. public function testOrWhereRaw(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE FIND_IN_SET(1, id) OR FIND_IN_SET(1, id)",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->whereRaw('FIND_IN_SET(1, id)')
  17. ->orWhereRaw('FIND_IN_SET(1, id)')
  18. ->findAll(true)
  19. )
  20. );
  21. }