【问题标题】:Select from two tables with same column name mysql从具有相同列名mysql的两个表中选择
【发布时间】:2020-01-03 06:30:16
【问题描述】:

我有两张桌子需要select * from。我正在构建一个搜索代码。

这是我的查询:

SELECT articles.*, news.* FROM articles, news WHERE title LIKE '%$search%' OR short_description LIKE '%$search%' OR author LIKE '%$search%' OR date LIKE '%$search%' LIMIT 15

我收到此错误:

Error
SQL query: Documentation


SELECT articles.*, news.* FROM articles, news WHERE title LIKE '%$search%' OR short_description LIKE '%$search%' OR author LIKE '%$search%' OR date LIKE '%$search%' LIMIT 15
MySQL said: Documentation

#1052 - Column 'title' in where clause is ambiguous

【问题讨论】:

  • WHERE articles.title LIKE ...
  • 错误信息的哪一部分你不明白?似乎很清楚。另外,您为什么不使用正确、明确、标准 JOIN 语法?
  • 解释一下 mysql 想对你说的话:你指的是哪个title?文章的标题?还是新闻标题?
  • 我明白了,但我需要他们每个人都提到他自己的标题我该怎么做?
  • 所以使用别名。

标签: php mysql sql database search


【解决方案1】:

试试这个?

SELECT a.*, n.* FROM articles a,
join news n on a.____ = n.______
WHERE a.title LIKE '%$search%' OR a.short_description LIKE '%$search%' 
OR n.author LIKE '%$search%' OR n.date LIKE '%$search%' 
LIMIT 15

您需要添加别名,我已经猜到了 LIKE 语句可能从每个表中提取的内容。您需要自己将 a.___ 替换为正确的标识符。

也可能值得更改旧的 JOIN 语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多