MySQL SELECT w/使用 php 的两个 AS id 字段名称,同时 mysql_fetch_array() 来唯一标识它们

发布于 2024-11-28 03:36:21 字数 561 浏览 1 评论 0原文

我正在运行一个 SELECT 查询,它有 a.id 和 c.id,如下所示:

$sql = "
(SELECT
a.id,
a.Title,
a.Category,
c.id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)"; // $CatID is defined earlier in     the script but is not pertinent to this problem.

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { echo $row['id'] // equals c.id }

但是 $row['id'] 是查询中调用的最后一个 id,并且计算结果为 c.id。我需要在 while 循环中获取 a.id 。

我的问题是如何在 while() 循环中唯一获取 a.id 和 c.id 的值?

I am running a SELECT query and it has a.id and c.id like so:

$sql = "
(SELECT
a.id,
a.Title,
a.Category,
c.id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)"; // $CatID is defined earlier in     the script but is not pertinent to this problem.

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { echo $row['id'] // equals c.id }

But $row['id'] is the last id called in the query and evaluates to c.id. I need to get a.id in my while loop.

My question is how do I get the value of a.id and c.id in my while() loop uniquely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遮了一弯 2024-12-05 03:36:21

c.id 列指定别名。

试试这个:

$sql = "
(SELECT
a.id,
a.Title,
a.Category,
c.id AS c_id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)"; 


$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { 
 echo $row['id'];
 echo $row['c_id'];
}

Give an alias to the c.id column.

Try this:

$sql = "
(SELECT
a.id,
a.Title,
a.Category,
c.id AS c_id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)"; 


$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { 
 echo $row['id'];
 echo $row['c_id'];
}
隱形的亼 2024-12-05 03:36:21
$sql = "
(SELECT
a.id as AID,
a.Title,
a.Category,
c.id as CID,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)";
$sql = "
(SELECT
a.id as AID,
a.Title,
a.Category,
c.id as CID,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)";
初相遇 2024-12-05 03:36:21
SELECT
  a.id AS aid,
  a.Title
  a.Category
  c.id AS cid,
  c.cateName
FROM ads_list a 
   LEFT JOIN ads_cate c ON c.id=a.Category WHERE a.Category = $CatID)

玩得开心

SELECT
  a.id AS aid,
  a.Title
  a.Category
  c.id AS cid,
  c.cateName
FROM ads_list a 
   LEFT JOIN ads_cate c ON c.id=a.Category WHERE a.Category = $CatID)

Have fun

天荒地未老 2024-12-05 03:36:21

您有两个选择:

1- 为每个 id 指定一个别名:

SELECT
a.id AS aid,
a.Title,
a.Category,
c.id AS cid,
...

并将它们命名为 $row[ 'aid' ] 和 $row[ 'cid' ]。

2-使用 mysql_fetch_row() 而不是 mysql_fetch_assoc() 并将它们寻址为 $row[0] 和 $row[3]。

You have two options:

1- give each id an alias:

SELECT
a.id AS aid,
a.Title,
a.Category,
c.id AS cid,
...

and address them as $row[ 'aid' ] and $row[ 'cid' ].

2- use mysql_fetch_row() instead of mysql_fetch_assoc() and address them as $row[0] and $row[3].

〃温暖了心ぐ 2024-12-05 03:36:21

您可以将查询更改为类似以下内容:

SELECT
a.id as a_id,
a.Title,
a.Category,
c.id as c_id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID

这样列名称中就没有歧义,您可以将其作为 $row['a_id'] 或 $row['c_id'] 访问

you can change your query to something like:

SELECT
a.id as a_id,
a.Title,
a.Category,
c.id as c_id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID

that way there is no ambiguity in column names and you can access it as $row['a_id'] or $row['c_id']

岁月染过的梦 2024-12-05 03:36:21

你不能从查询中删除 c.id 吗?如果您在查询中需要它,则在 SELECT 语句中不需要它:

$sql = "SELECT a.id, a.Title, a.Category, c.cateName 
FROM ads_list AS a 
LEFT JOIN ads_cate AS c ON c.id=a.Category 
WHERE a.Category = $CatID"; 

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { echo $row['id'] // equals c.id }

如果您需要它,您也可以使用 PDO 和值绑定。我不建议使用 mysql_* 函数。 PDO 更好、更容易阅读:

  $query = $pdo->query('SELECT a.id, a.Title, a.Category, c.id, c.cateName 
                               FROM ads_list AS a 
                               LEFT JOIN ads_cate AS c ON c.id=a.Category 
                               WHERE a.Category = $CatID');

  $query->bindColumn(1, $id_a); //numerical index goes from 1
  $query->bindColumn(4, $id_b);
  $query->bindColumn('Title', $title); //you can also index using column name if it's possible

  while ($query->fetch(PDO::FETCH_BOUND)) //fetch returns true if there are any records to fetch
  {
    echo $id_a;
    echo $id_b;
    echo $title;
  }

该解决方案(和 mysql_fetch_row)的缺点是查询中的任何更改都可能导致索引更改。即使现在这不是问题,请相信我 - 我已经看到一个包含 30 个返回列并应用了 mysql_fetch_row 的查询。这是一场噩梦。无法找出数据库中的哪一列由某个变量表示。

Can't you just remove c.id from query? You don't need that within SELECT statement if you need it in your query:

$sql = "SELECT a.id, a.Title, a.Category, c.cateName 
FROM ads_list AS a 
LEFT JOIN ads_cate AS c ON c.id=a.Category 
WHERE a.Category = $CatID"; 

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { echo $row['id'] // equals c.id }

If you need it though, you can also use PDO and value binding. I do not recommend using mysql_* functions. PDO is just better and easier to read:

  $query = $pdo->query('SELECT a.id, a.Title, a.Category, c.id, c.cateName 
                               FROM ads_list AS a 
                               LEFT JOIN ads_cate AS c ON c.id=a.Category 
                               WHERE a.Category = $CatID');

  $query->bindColumn(1, $id_a); //numerical index goes from 1
  $query->bindColumn(4, $id_b);
  $query->bindColumn('Title', $title); //you can also index using column name if it's possible

  while ($query->fetch(PDO::FETCH_BOUND)) //fetch returns true if there are any records to fetch
  {
    echo $id_a;
    echo $id_b;
    echo $title;
  }

Downside of this solution (and mysql_fetch_row) is that any change within the query can result in change of indexes. And even if now it's not the problem, believe me - I've seen a query with 30 returned columns and mysql_fetch_row applied. It was a nightmare. Impossible to find out which column in database is represented by certain variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文