将变量与SELECT结果组合成INSERT语句

时间:2022-01-30 12:02:45

I have 3 tables:

我有3张桌子:

Users - uID (INT AUTO_INCREMENT), name (VARCHAR)

用户 - uID(INT AUTO_INCREMENT),名称(VARCHAR)

Movies - mID (IN AUTO_INCREMENT), title (VARCHAR)

电影 - mID(IN AUTO_INCREMENT),标题(VARCHAR)

Watched - uID (INT), mID (INT)

看了 - uID(INT),mID(INT)

I'm writing a php function that constructs a query which adds records of movies watched by a particular person. Here's what I've come up with so far:

我正在编写一个php函数来构造一个查询,该查询添加了特定人观看的电影记录。这是我到目前为止所提出的:

function set_watched($name, $movies){
    $sql = "SET @userid = (SELECT uID FROM users WHERE name = '$name' LIMIT 1); ";
    $sql .= "INSERT INTO watched (uID, mID) VALUES ('";

    foreach ($movies as $index => $movie){

    }

}

My question: Is there a way to combine the @userid variable with the results of a SELECT mID FROM MOVIES WHERE title = $movie OR title = $movie [generated with foreach]?

我的问题:有没有办法将@userid变量与SELECT mID FROM MOVIES WHERE title = $ movie OR title = $ movie [使用foreach生成]的结果相结合?

I don't want to generate separate SELECT statements for every movie title. Perhaps I don't even have to use the @userid variable at all?

我不想为每个电影标题生成单独的SELECT语句。也许我根本不需要使用@userid变量?

2 个解决方案

#1


2  

Try something like this:

尝试这样的事情:

$sql = "INSERT INTO watched (uID, mID)
  SELECT User.uID, Movies.mID
  FROM (SELECT uID FROM Users WHERE Users.name = '$name' LIMIT 1) AS User, Movies      
  WHERE ";

foreach ($movies as $index => $movie){
  $sql .= "Movies.title = '$movie' OR ";
}

$sql = substr($sql, 0, -4) . ";";

#2


1  

I prefer using arrays and imploding them for this sort of an application. Also, I wouldn't try and force these two things into one query. I would either:

我更喜欢使用数组并将它们插入到这种应用程序中。另外,我不会尝试将这两个内容强制转换为一个查询。我要么:

  1. Modify the function parameters to accept uID as its input, instead of name
  2. 修改函数参数以接受uID作为其输入,而不是名称
  3. Change the logic to two queries.
  4. 将逻辑更改为两个查询。

Besides, PHP's mysql_query function doesn't support multiple queries, so if you're using the standard mysql functions, you can't execute two queries with one call to mysql_query.

此外,PHP的mysql_query函数不支持多个查询,因此如果您使用的是标准的mysql函数,则只需调用一次mysql_query即可执行两个查询。

Running with case #2, you can use something like this (untested, of course):

使用案例#2运行,您可以使用类似的东西(当然未经测试):

$sql = 'SELECT uID FROM users WHERE name = "' . $name. '" LIMIT 1';
$result = mysql_query( $sql);
$row = mysql_fetch_row( $result);
mysql_free_result( $result);

$values_array = array();
foreach ($movies as $index => $movie)
{
     $values_array[] = '( "' . $row['uID'] . '", "' . $movie . '")';
}

$sql = 'INSERT INTO watched (uID, mID) VALUES ' . implode( ', ', $values_array);
$result = mysql_query( $sql);

#1


2  

Try something like this:

尝试这样的事情:

$sql = "INSERT INTO watched (uID, mID)
  SELECT User.uID, Movies.mID
  FROM (SELECT uID FROM Users WHERE Users.name = '$name' LIMIT 1) AS User, Movies      
  WHERE ";

foreach ($movies as $index => $movie){
  $sql .= "Movies.title = '$movie' OR ";
}

$sql = substr($sql, 0, -4) . ";";

#2


1  

I prefer using arrays and imploding them for this sort of an application. Also, I wouldn't try and force these two things into one query. I would either:

我更喜欢使用数组并将它们插入到这种应用程序中。另外,我不会尝试将这两个内容强制转换为一个查询。我要么:

  1. Modify the function parameters to accept uID as its input, instead of name
  2. 修改函数参数以接受uID作为其输入,而不是名称
  3. Change the logic to two queries.
  4. 将逻辑更改为两个查询。

Besides, PHP's mysql_query function doesn't support multiple queries, so if you're using the standard mysql functions, you can't execute two queries with one call to mysql_query.

此外,PHP的mysql_query函数不支持多个查询,因此如果您使用的是标准的mysql函数,则只需调用一次mysql_query即可执行两个查询。

Running with case #2, you can use something like this (untested, of course):

使用案例#2运行,您可以使用类似的东西(当然未经测试):

$sql = 'SELECT uID FROM users WHERE name = "' . $name. '" LIMIT 1';
$result = mysql_query( $sql);
$row = mysql_fetch_row( $result);
mysql_free_result( $result);

$values_array = array();
foreach ($movies as $index => $movie)
{
     $values_array[] = '( "' . $row['uID'] . '", "' . $movie . '")';
}

$sql = 'INSERT INTO watched (uID, mID) VALUES ' . implode( ', ', $values_array);
$result = mysql_query( $sql);