douliang9057 2012-05-04 10:27
浏览 77

请帮我处理我的评论部分sql查询

OK guys, I am in need of a little help here! Using PHP and mysql, I made a comment section that users can add comments to. First off, the page is based off of the ItemID and displays the correct item. When they add a comment, I take their username and the ItemID and put it in the comment table as comment.username and messageailmentID. So the page displays correctly, and the comment section works great, except that I cannot seem to get the SQL query right. I have tried many variations, with no luck. I want the comment that anyone posts under the pages ItemID to show only those comments under that ItemID (specifically messageailmentID).

MY query ranges, but here is the one I have at the moment:

SELECT `comment`.messageID, `comment`.message, `comment`.username, `comment`.messageailmentID, Items.ItemID
FROM `comment`, Items
WHERE varItemID = `comment`.messageailmentID

(Using dreamweaver, the varItemID is basically $_GET['Items.ItemID'])

EDIT: As an example, The user clicks an item which takes them to the itemdetails.php?ItemID=8. When they leave a comment, it will add Items.ItemID into comment.messageailmentID.

The important part of the table columns are this:

Items: ItemID

comment: CommentID, message, comment.username, messageailmentID(which is the pages ItemID)

The varItemID isn't a column, but does $_GET['ItemID'], which may not even be needed in this query because it is done on another query.

  • 写回答

3条回答 默认 最新

  • donglianjiang9321 2012-05-04 10:32
    关注

    Your query is fetching all rows.

    You should use:

    • A JOIN clause to specify the relationship between the tables.
    • A WHERE clause to specify which particular items you want to fetch.

    Try this:

    SELECT
        comment.messageID,
        comment.message,
        comment.username,
        comment.messageailmentID,
        Items.ItemID
    FROM Items
    LEFT JOIN comment
    ON Items.ItemID = comment.messageailmentID
    WHERE Items.ItemID = 42
    

    Using a left join means that it will still return one row even if there are no comments. Use INNER JOIN if you don't want this.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c