1

各項目の URL と説明を保持するテーブルを追加しました。以下のクエリは問題なく動作します...しかし、それはURLを返すだけで、descも必要です。これを機能させる方法がわかりません。

$query_str = "SELECT      a.userid, a.cat, a.id, a.name, a.image, 
                          a.desc, a.country, b.user_id, c.username, 
                          c.fbook, d.cat_id,
                          (
                               SELECT GROUP_CONCAT(url) 
                               FROM one_add o 
                               WHERE a.id=o.one_id

                          ) AS url,
                          (
                               SELECT COUNT(id) 
                               FROM one_msg m 
                               WHERE m.guest_id = ? AND 
                                     m.one_id=a.id 
                          ) AS count
               FROM place a
                            LEFT OUTER JOIN wait b 
                                 ON a.id=b.post_id AND 
                                    b.user_id= ?
                            JOIN users c
                                 ON a.userid=c.id
                            LEFT JOIN ( 
                                       SELECT userid, user_id, 
                                              GROUP_CONCAT( cat_id ) AS cat_id 
                                       FROM user_cat 
                                       WHERE userid='$user_id' 
                                       GROUP BY user_id 
                                      ) AS d ON d.userid='$user_id' AND 
                                                d.user_id=a.userid
              WHERE a.cat != ? ORDER BY a.date desc";

これは私が達成したいことです: <a href="url">desc</a>

4

1 に答える 1

1

CONCAT内部GROUP_CONCAT関数を次のように使用することで、これを実現できます。

(SELECT GROUP_CONCAT(CONCAT('<a href=\"',url,'\">',desc,'</a>')) 
 FROM one_add o 
 WHERE a.id=o.one_id) AS url

完全なクエリ:

(SELECT COUNT(id) FROM one_msg m WHERE m.guest_id = ? AND m.one_id=a.id ) AS count
FROM place a
LEFT OUTER JOIN wait b ON a.id=b.post_id AND b.user_id= ?
JOIN users c ON a.userid=c.id
LEFT JOIN ( SELECT userid, user_id, GROUP_CONCAT( cat_id ) AS cat_id FROM user_cat WHERE userid='$user_id' GROUP BY user_id ) AS d ON d.userid='$user_id' AND d.user_id=a.userid
WHERE a.cat != ? ORDER BY a.date desc";
于 2012-08-01T12:07:10.313 に答える