2012-01-08 115 views
0

我有这个查询工作得很好,但是我想改变我的数据库表一点,所以我需要改变我的查询一点点。我的工作的查询是这样的:MYSQL从数据库中的其他表中选择,匹配数据等

$notificationsq = mysql_query(" 
SELECT 
    N.*, 
    P.*, 
MAX(N.date) AS newDate 


FROM 
    notifications N, 
    posts P 

WHERE 
    N.userID='$session' 
AND 
    (
      (
        N.action='1' 
       AND 
        (N.state = 0 OR N.state=1) 
      ) 
      OR 
       N.action='2' 
    ) 
AND P.state='0' 
AND 
    N.uniqueID=P.id 
GROUP BY 
    N.uniqueID 
ORDER BY 
    N.state ASC, 
    newDate DESC 

") or die(mysql_error()); 

现在,我在做什么正在改变行“UNIQUEID”,所以我需要做的基本上是:

if(action==2){unqiqueID=C.postID} else {uniqueID=N.uniqueID} 

这里是我的表结构: i41.tinypic.com/nyzolg.png


的信息从照片是:

Table: Notifications 

id UserID FromID UniqueID Action State Read_Date Date 
1 1  2  1   1  0  0   1325993600 
2 1  6  2   1  0  0   1325993615 
3 1  2  1   2  0  0   1325993622 
4 1  6  2   2  0  0   1325993661 
5 2  6  2   2  0  0   1325993661 

Action = 1表示UniqueID标识Posts中的一行; Action = 2表示UniqueID标识评论中的一行。

Table: Posts 

id ToID FromID Post  State Date 
1 1  2  Hey   0  1325993600 
2 1  6  okay yeah 0  1325993615 

Table: Comments 

ID PostID FromID Comment  State Date 
1 1  2  lol   0  1325993622 
2 1  6  ohh   0  1325993661 

因此,在操作为2的通知表中,UniqueID是针对注释表中的'id'的。 我想回的是帖子ID,所以在查询这纯粹是因为如果是的UniqueID这个:

1 
2 
1 
1 
1 

但是,在动作为1

回答

2

你不能在同一个SELECT语句的WHERE子句中引用计算列。通常,这可以通过使用子查询像这样被工作围绕:

SELECT 
    * /* or list the necessary columns specifically */ 
FROM (
    SELECT 
    ... 
    some_expression AS columnAlias, 
    ... 
    FROM ... 
) 
WHERE ... 
    AND columnAlias = ... 

正如你所看到的,通过some_expressioncolumnAlias计算列,可以在外部选择使用它的别名来访问。

但是你在这里拉了很多列,并且也使用了遮罩,所以试图使用子查询可能会导致名称冲突。尝试通过明确指定来减少要拖动的列数。


UPDATE

基于在评论进一步解释,这里就是我可能会做在您的情况:

SELECT 
    P.id AS uniqueID2, 
    MAX(N.Date) AS newDate, 
    MIN(state) AS state,   /* or maybe 'AS minState', if you like */ 
    MAX(N.Read_Date) AS lastRead, /* just another example to illustrate the 
             point that most columns in this query 
             should be selected with aggregating */ 
    ... /* other columns as necessary */ 
FROM notifications N 
    LEFT JOIN comments C ON N.action = 2 AND N.uniqueID = C.id 
    INNER JOIN posts P ON N.action = 1 AND P.id = N.uniqueID 
         OR N.action = 2 AND P.id = C.postID 
WHERE N.userID = '$session' 
    AND (N.action = 1 AND N.state IN (0, 1) OR N.action = 2) 
    AND P.state = 0 
GROUP BY P.id 
ORDER BY 
    state ASC, /* No 'N.' prefix here because now it is a reference 
        to the MIN(state) column, which has the same name. 
        If it looks too confusing, use a different alias for 
        the column and change this entry accordingly. */ 
    newDate DESC 
+0

谢谢,您能否使用我的代码将某些东西放在一起以向我展示您的意思?这将是非常感谢。 – 2012-01-08 00:54:13

+0

@DylanCross:说实话,您的查询目前对我来说没有什么意义。我可以看到,您正在尝试使用'C.postID'或'N.uniqueID'来匹配'P.id',具体取决于特定条件。这是让我最困惑的状况。你能解释一下你想做什么吗?我可能会提出一个不同的方法,但首先我想了解那里发生了什么。 – 2012-01-08 04:24:42

+0

好吧,N.uniqueID是以前用来存储C.postID的地方。现在它是注释表中注释的唯一标识。这是一张照片。 http://i41.tinypic.com/nyzolg.png – 2012-01-08 04:34:40

0

尝试的UniqueID将保持不变把uniqueID2 = P.id放在HAVING块中,而不是放在WHERE块中。我相信它会起作用。

大卫·莱昂斯, ehtu.com

+0

谢谢,这类作品,但它只是返回1结果应该至少有3个或4个。 – 2012-01-08 00:55:02

相关问题