如何显示至少有一张已批准照片的相册

时间:2015-05-08 11:19:31

标签: postgresql

我有两个表:一个是ËAXnAhÆ0àŠ0ðº0@BÐDA€è0@Ã0xŠAXò0ÐêA€òAÐÅAh‘0¨LA¨¸0ðìA@¸A «Ap¢AXAøÓ0ˆÎ0ðçAÀ†0 ¶AÈoAðaA,另一个是//http://YOURSITE.COM/index.php?option=com_hoicoiapi&task=registration&name=NAME&username=USERNAME&passwd=PASSWORD&email=EMAIL public function registration() { $name = JRequest::getVar('name'); $username = JRequest::getVar('username'); $passwd = JRequest::getString('pass'); $email = JRequest::getVar('email'); $data = array( "name"=>$name, "username"=>$username, "password"=>$passwd, "password2"=>$passwd, "email"=>$email, "block"=>1, "groups"=>array("2"), "sendEmail"=>("1"), ); $user = new JUser; //Write to database if(!$user->bind($data)) { $status = "Could not bind data. Error: " . $user->getError(); } if (!$user->save()) { $status = "Could not save user. Error: " . $user->getError(); } else { $status = "Success"; } $message = array( 'message' => $status ); header('Content-Type: application/json'); echo json_encode ($message); jexit(); } 。我需要显示至少有一张已批准照片的相册。我有以下表格字段:

     //http://YOURSITE.COM/index.php?option=com_hoicoiapi&task=comment&author=AUTHOR&email=EMAIL&content=CONTENT
   public function comment()
  {
  $author = JRequest::getVar('author');
  $email = JRequest::getVar('email');
  $content = JRequest::getVar('content');


  $data = array(
       "author"=>$author,
       "email"=>$email,
       "content"=>$content,

        );

   $comment = new comment;
   //Write to database
   if(!$comment->bind($data)) {
       $status = "Could not bind data. Error: " . $user->getError();
    }
    if (!$comment->save()) {
      $status = "Could not save user. Error: " . $user->getError();
    }
    else {
     $status = "Success";
    }

    $message = array(
       'message' => $status
       );

    header('Content-Type: application/json');
    echo json_encode ($message);
    jexit();
}

在上表中,只有身份1(“myalbum”)的专辑已经批准了照片,其他两张专辑没有任何已批准的照片。所以我想要一个查询,显示至少有一张已批准照片的相册名称。

我试过这样:

album

4 个答案:

答案 0 :(得分:1)

以下查询应该返回至少有一张照片获得批准的相册(蚂蚁所有照片是否已批准)。

select a.*,a.id as album_id,ap.*  
from (select a1.* from album a1 where a1.id in (select distinct ap1.album_id    from album_photo ap1 where ap1.status='Approved')) a,  album_photo ap
where a.id=ap.album_id;

如果您只想要没有可以使用照片的相册

select a1.* from album a1 where a1.id in (select distinct ap1.album_id     from album_photo ap1 where ap1.status='Approved')

答案 1 :(得分:1)

尽管在开始时加入它们只是在where子句中加入它们,你的查询有点错误。

select a.*,a.id as album_id from album a
where (select distinct ap.album_id  from album_photo ap where 
ap.album_id=a.id and ap.status='Approved')>0;

休息所做的一切都是正确的。 另一个解决方案是没有区别的,因为我们知道ID总是唯一的,因此内部查询中的不同可以被删除,并且可以如下编写。

select a.*,a.id as album_id from album a
where  (select count(*)  from album_photo ap where 
ap.album_id=a.id and ap.status='Approved')>0;

答案 2 :(得分:1)

SELECT *
FROM album a
WHERE EXISTS (
    SELECT 1
    FROM album_photo ap
    WHERE ap.album_id = a.id AND ap.status = 'Approved'
);

EXISTS产生一个半连接,可以期望它比连接更好。

答案 3 :(得分:0)

SELECT a.id AS album_id, a.name, a.date, ap.id AS photo_id, ap.photo_url, ap.status
FROM album a
JOIN album_photo ap ON ap.album_id = a.id
WHERE ap.album_id IN 
  (SELECT DISTINCT album_id
   FROM album_photo
   WHERE status = 'Approved');

子查询查找至少已批准1张照片的所有相册的ID。然后,主要查询会显示所有相册中的所有照片详细信息。