Mysql使用值插入查询并选择为子查询

时间:2014-09-18 05:02:54

标签: mysql sql select

我尝试了很多解决方案,但无法让我的查询工作。

我正在尝试创建一个类似的系统,在代码中会检查所喜欢的项目是否存在,并且尚未被喜欢。

我认为我的查询很接近,但是我一直收到错误。

变量“$ this_is_liked”预定义为值“是”,变量“$ likes_user_id”预定义为登录用户的值(本例中为1),然后“$ likes_item_id”被拉出从pms_inventory表中,我正在尝试检查项目ID是否存在,并且它尚未被喜欢。

下面是我的代码,然后是错误。

        if(isset($_POST['type'], $_POST['itemid'], $_POST['liked']))  {
            include_once('connectdb.php');
            include_once('global.php');
            $liked_item_id = (int)$_POST['itemid']; // Liked Item ID
            $liked_user_id = (int)$_SESSION['id']; // User ID
            $this_is_liked = $_POST['liked']; // Value of "Yes"
        mysql_query("
        INSERT INTO pms_inventory_likes 
            (
            liked,
            user_id, 
            item_id, 
            )
            VALUES
            (
            SELECT 
            {$this_is_liked}, {$liked_user_id}, {$liked_item_id} 
            FROM pms_inventory
            WHERE EXISTS (
                SELECT id 
                FROM pms_inventory
                WHERE id = {$liked_item_id})
            AND NOT EXISTS (
                SELECT id 
                FROM pms_inventory_likes 
                WHERE user_id = {$liked_user_id} 
                AND item_id = {$liked_item_id} 
                AND liked = 'Yes')  
            )
            LIMIT 1
        ") or die(mysql_error());
    }

这是错误:

    <span>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
            VALUES
            (
            SELECT 
            Yes, 1, 40 
            FROM pms_inventory
            ' at line 6</span>

我不确定我是否正确嵌套“SELECT”,或者我是否正确标记了值。

如果需要更多信息,请告诉我们!

2 个答案:

答案 0 :(得分:0)

试试这可能对你有帮助......

              INSERT INTO pms_inventory_likes 
                (liked,
                user_id, 
                item_id) 

               SELECT {$this_is_liked}, {$liked_user_id}, {$liked_item_id} FROM pms_inventory

                WHERE EXISTS (
                    SELECT id 
                    FROM pms_inventory
                    WHERE id = {$liked_item_id})

                AND NOT EXISTS (
                    SELECT id 
                    FROM pms_inventory_likes 
                    WHERE user_id = {$liked_user_id} 
                    AND item_id = {$liked_item_id} 
                    AND liked = "Yes")  
                LIMIT 1

如需更多参考,请参阅以下链接:

Oracle insert if not exists statement

http://www.techonthenet.com/oracle/insert.php

http://docs.oracle.com/cd/E17952_01/refman-5.1-en/exists-and-not-exists-subqueries.html

答案 1 :(得分:0)

在上面两个贡献者的帮助下,我能够找到问题的最终根源...... 同时删除&#34; VALUES&#34;声明的一部分,并在#39; {$ this_is_liked}&#39; (谢谢Senk和K.Raj)中添加单引号,像往常一样,这里我们似乎都错过了一个微小的语法错误最终让它发挥作用:

    INSERT INTO pms_inventory_likes 
        (
        liked,
        user_id, 
        **item_id,** 
        )

在item_id之后有一个逗号,它使代码无法运行。对于那些想要使用此代码制作一个简单的&#34; Like&#34;系统为您的网站,代码的最终,工作版本是:

    if(isset($_POST['type'], $_POST['itemid'], $_POST['liked']))  {
        include_once('connectdb.php');
        include_once('global.php');
        $liked_item_id = (int)$_POST['itemid']; // Liked Item ID
        $liked_user_id = (int)$_SESSION['id']; // User ID
        $this_is_liked = $_POST['liked']; // Value of "Yes"
    mysql_query("
    INSERT INTO pms_inventory_likes 
        (
        liked,
        user_id, 
        item_id
        )
        SELECT 
        '{$this_is_liked}', {$liked_user_id}, {$liked_item_id} 
        FROM pms_inventory
        WHERE EXISTS (
            SELECT id 
            FROM pms_inventory
            WHERE id = {$liked_item_id})
        AND NOT EXISTS (
            SELECT id 
            FROM pms_inventory_likes 
            WHERE user_id = {$liked_user_id} 
            AND item_id = {$liked_item_id} 
            AND liked = 'Yes')  
        LIMIT 1
    ") or die(mysql_error());
}

数据库中的表格必须是id,user_id,item_id和喜欢。

我希望这有助于某人!