1

在成功连接到数据库后,我正在尝试使用以下语句从我的 mysql 数据库中选择信息:

$query = "SELECT * FROM `users`, `markers`,`imagemarkers` 
    WHERE username LIKE '%$s%' 
    OR email LIKE '%$s%' 
    OR location LIKE '%$s%' 
    OR author LIKE '%$s%' 
    OR bike LIKE '%$s%' 
    OR id LIKE '%$s%' 
    OR title LIKE '%$s%'";

我收到此错误:where 子句中的列“作者”不明确。我理解这是因为我有多个具有相同字段名称的表。

我想从这些表和这些字段中提取信息:

 -markers:
    author
    title
    bike
    id
    date

 -imagemarkers:
   -author
   -title
   -id
   -date

 -users:
   -loction
   -email
   -username
   -id

我已经搜索了解决方案,并且到目前为止得出的结论是,每个表字段都应该被称为:

  markers.title
  imagemarkers.title

  markers.author
  imagemarkers.author

  markers.date
  imagemarkers.date

  markers.id
  imagemarkers.id
  users.id

该声明可能类似于:

  SELECT markers.author
    FROM markers JOIN imagemarkers ON markers.author = imagemarkers.author

但我不确定如何使用我需要检索的大量信息来完成这项工作。

我目前拥有的整个代码如下所示:

        if (isset($_POST['submit'])) {
    if ($_POST['search'] !="") {
    require("connection.php");

    $s = mysql_real_escape_string($_POST['search']);
    $query = "SELECT * FROM `users`, `markers`,`imagemarkers` WHERE username LIKE '%$s%' OR email LIKE '%$s%' OR location LIKE '%$s%' OR author LIKE '%$s%' OR bike LIKE '%$s%' OR id LIKE '%$s%' OR title LIKE '%$s%'";
    $result = mysql_query($query, $connect)
        or die(mysql_error());

    $num = mysql_num_rows($result);

    echo "<h2>you searched for: " . $s . "..</h2>";
    echo "<h5>there are " . $num . " results</h4>";

while ($row = mysql_fetch_assoc($result)) {
    echo "<p>username: " . $row['username'] . "<br />";
    echo "location: " . $row['location'] . "</p>";
    echo "author: " . $row['author'] . "</p>";//error: Column 'author' in where clause is ambiguous, same with date. 
    echo "date: " . $row['date'] . "</p>";
    echo "id: " . $row['id'] . "</p>";
    echo "title: " . $row['title'] . "</p>"; 
    echo "<hr />";

    }
} else {
        echo "<h3> you must type something in the box</h3>";
}  }

谁能给我任何帮助?

非常感谢。

4

3 回答 3

1

那是因为字段的命名方式相同

尝试:

$query = "SELECT * FROM `users` u, `markers` m,`imagemarkers` im
    WHERE u.username LIKE '%$s%' 
    OR u.email LIKE '%$s%' 
    OR u.location LIKE '%$s%' 
    OR m.author LIKE '%$s%' 
    OR m.bike LIKE '%$s%' 
    OR m.title LIKE '%$s%'";

等等

于 2011-05-05T11:04:34.873 回答
0

又快又脏

$query = "SELECT * FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id) 
 WHERE u.username LIKE '%$s%'
  OR u.email LIKE '%$s%'
  OR u.location LIKE '%$s%'
  OR m.author LIKE '%$s%'
  OR m.bike LIKE '%$s%'
  OR m.title LIKE '%$s%'"; 

这会将结果链接在一起,以便仅显示与用户相关的标记和图像标记。

请注意,我不知道markers.author 是链接到此查询的integer字段users.id还是链接到此查询的char 字段users.name假定前者,请相应地调整连接条件,这与您的表不匹配。

如果你省略了连接条件,你最终会得到一个交叉连接,这 99% 可能不是你想要的。
见:http ://en.wikipedia.org/wiki/Join_(SQL )

显式列出列
如果要限制列,则必须显式命名它们(推荐)。

$query = "SELECT u.location, u.email, u.username as author, u.id as user_id
   ,m.title as marker_title, m.bike, m.date as marker_date, m.id as marker_id
   ,im.title as imagemarker_title, im.date as imagemarker_date, im.id as imagemarker_id  
FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id) 
 WHERE u.username LIKE '%$s%'
  OR u.email LIKE '%$s%'
  OR u.location LIKE '%$s%'
  OR m.author LIKE '%$s%'
  OR m.bike LIKE '%$s%'
  OR m.title LIKE '%$s%'"; 

请注意,由于所有内容都链接到作者,因此您只需要列出该字段一次,否则您将获得 3 个内容完全相同的作者字段,这将是愚蠢的。

于 2011-05-05T11:55:47.997 回答
0

这是一个简单问题的大量代码。模棱两可意味着您的选择案例中的标识符或案例存在于多个表中的情况。如果您说“选择作者”,则需要从哪个表中指定..所以markers.authors。如果你在做“作者在哪里……”你需要“在哪里标记。作者”

我建议 UNIONing 你的桌子。

于 2011-05-05T10:59:03.670 回答