gpt4 book ai didi

Mysql 插入带有值的查询并选择作为子查询

转载 作者:行者123 更新时间:2023-11-29 23:46:32 24 4
gpt4 key购买 nike

我尝试了多种解决方案,但无法使我的查询正常工作。

我正在尝试创建一个点赞系统,该系统在代码中将检查被点赞的项目是否存在以及尚未被点赞。

我认为我已经接近查询,但我不断收到错误。

变量“$this_is_liked”预定义为值“Yes”,变量“$liked_user_id”预定义为登录用户的值(本例中为1),然后“$liked_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”,或者是否正确标记了值。

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

最佳答案

试试这个,它可能会帮助你......

              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

关于Mysql 插入带有值的查询并选择作为子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25904484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com