PHP-PDO运行多个选择查询

时间:2019-02-23 22:31:20

标签: php mysql sql pdo

我正在尝试使用来自几个不同表格的信息填充Google地图。

Traceback (most recent call last):
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: Leagues

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 244, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 268, in __iter__
    self._fetch_all()
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/Tilley/PycharmProjects/SportWeb/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: Leagues

我保存了所需的信息并为每个结果绘制一个地图标记。但是我没有从第二个查询中得到结果。还有另一种方法可以运行多个查询吗?我最终希望一次运行两个以上。

配置:

  • PHP版本:5.6 +
  • PDO :: ATTR_EMULATE_PREPARES = 1

1 个答案:

答案 0 :(得分:0)

从评论中,我怀疑您可以选择两个表共有的列列表,并使用UNION ALL在单个查询中获取所有信息:

SELECT Latitude, Longitude, ...
FROM Contractors 
WHERE `Name` = 'John'
UNION ALL
SELECT Latitude, Longitude, ...
FROM Interns 
WHERE `Name` = 'Mike'

请记住,两个联合查询必须返回具有相同数据类型的相同列列表(除非MySQL对此松懈)。 SQL中的一般要求是,查询必须返回固定的一组列,且每一列中的数据类型都一致。