mysql 查询仅在行数大于零时返回

发布于 2024-12-28 02:38:52 字数 633 浏览 0 评论 0原文

如何更改此查询以仅返回返回多于 0 行的行?

我需要结合这两个查询。我需要返回第二个查询至少返回 1 行的行。

    SELECT albumData.* 
       FROM albumData 
      WHERE userID='$id' 
        AND state='0' 
   ORDER BY date DESC


  SELECT photos.photo 
    FROM albums, photos 
   WHERE albums.userID = '$id' 
     AND albums.photoID = photos.id 
     AND albums.albumID = $albumID 
ORDER BY photos.id DESC LIMIT 6;

编辑从评论中添加有用信息:

表albumData 为每个用户、每张照片存储唯一的行 他们已经设置了专辑,这是我要返回专辑数量的地方 从最初开始的行。不过我想将 albumData 中的 id 与 albumID 是相册表,如果行数带有 albums.albumID=albumData.id 大于 1,然后返回这些行。

How can I change this query to only return rows that return more than 0 rows?

I need to combine these two queries. I need to return rows where the second query returns at least 1 row.

    SELECT albumData.* 
       FROM albumData 
      WHERE userID='$id' 
        AND state='0' 
   ORDER BY date DESC


  SELECT photos.photo 
    FROM albums, photos 
   WHERE albums.userID = '$id' 
     AND albums.photoID = photos.id 
     AND albums.albumID = $albumID 
ORDER BY photos.id DESC LIMIT 6;

EDIT add useful information from comments:

Table albumData containers unique rows, for each user, for each photo
album they have set up, this is where I want to return the number of
rows from initially. However I want to compare the id in albumData to
albumID is albums table, if the number of rows with
albums.albumID=albumData.id is more than one, then return those rows.

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

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

发布评论

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

评论(4

故人的歌 2025-01-04 02:38:52

下次尝试..如果我理解问题正确的话

SELECT photos.photo 
FROM albums
INNER JOIN photos on albums.photoID = photos.id 
INNER JOIN albumData ON albumData.id = albums.albumID
WHERE albums.userID = '$id' 
  and albums.albumID = $albumID 
  and albumData.state = '0'
ORDER BY photos.id DESC LIMIT 6;

next try .. if I understand the problem right

SELECT photos.photo 
FROM albums
INNER JOIN photos on albums.photoID = photos.id 
INNER JOIN albumData ON albumData.id = albums.albumID
WHERE albums.userID = '$id' 
  and albums.albumID = $albumID 
  and albumData.state = '0'
ORDER BY photos.id DESC LIMIT 6;
从﹋此江山别 2025-01-04 02:38:52

我假设你遇到了某种错误..
如果你试图阻止空记录集:

使用这样的东西

$getPhotosq = "
SELECT photos.photo FROM albums, photos 
WHERE albums.userID='$id' 
AND albums.photoID=photos.id 
AND albums.albumID=$albumID 
ORDER BY photos.id DESC LIMIT 6"; 

$rs = mysql_query($getPhotosq);
if($rs) {
 /// DO THE CODE
}

Im going to assume your getting some kind of error..
if your trying to prevent a null recordset:

use someything like this

$getPhotosq = "
SELECT photos.photo FROM albums, photos 
WHERE albums.userID='$id' 
AND albums.photoID=photos.id 
AND albums.albumID=$albumID 
ORDER BY photos.id DESC LIMIT 6"; 

$rs = mysql_query($getPhotosq);
if($rs) {
 /// DO THE CODE
}
听风念你 2025-01-04 02:38:52

您的查询似乎正确

下面可能会出现问题...

没有记录等于$idAND$albumID。请先手动检查并再次执行查询...

祝你好运!!!

Your query seems CORRECT.

Below might be problem...

There are NO records that equal $id AND $albumID. Please check this manually first and execute query again...

Good Luck!!!

假装不在乎 2025-01-04 02:38:52
  SELECT albumData.* 
    FROM albumData, albums 
   WHERE albumData.userID='$id' 
     AND albumData.state='0' 
     AND albums.state='0' 
     AND albumData.id=albums.albumID 
GROUP BY albums.albumID 
ORDER BY albums.date DESC
  SELECT albumData.* 
    FROM albumData, albums 
   WHERE albumData.userID='$id' 
     AND albumData.state='0' 
     AND albums.state='0' 
     AND albumData.id=albums.albumID 
GROUP BY albums.albumID 
ORDER BY albums.date DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文