假设我有一个带有名为$fighter1 = $_POST["fighter1"];
$fighter2 = $_POST["fighter2"];
$fighter3 = $_POST["fighter3"];
$fighter4 = $_POST["fighter4"];
$fighter5 = $_POST["fighter5"];
/********************** CONNECTION ***********************/
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{ die("Connection Failed: " . $conn->connect_error);}
/************************ UPDATES ************************/
if ($fighter1 == "Conor McGregor")
{ $sql = "UPDATE Predictions_Poll SET fighter_one_votes = fighter_one_votes + 1 WHERE id = 1"; }
else if ($fighter1 == "Khabib Nurmagomedov")
{ $sql = "UPDATE Predictions_Poll SET fighter_two_votes = fighter_two_votes + 1 WHERE id = 1"; }
if ($fighter2 == "Georges St‑Pierre")
{ $sql = "UPDATE Predictions_Poll SET fighter_one_votes = fighter_one_votes + 1 WHERE id = 2"; }
else if ($fighter2 =="Tyron Woodley")
{ $sql = "UPDATE Predictions_Poll SET fighter_two_votes = fighter_two_votes + 1 WHERE id = 2";}
if ($fighter3 == "Israel Adesanya")
{ $sql = "UPDATE Predictions_Poll SET fighter_one_votes = fighter_one_votes + 1 WHERE id = 3"; }
else if ($fighter3 =="Kelvin Gastelum")
{ $sql = "UPDATE Predictions_Poll SET fighter_two_votes = fighter_two_votes + 1 WHERE id = 3"; }
if ($fighter4== "Francis Ngannou")
{ $sql = "UPDATE Predictions_Poll SET fighter_one_votes = fighter_one_votes + 1 WHERE id = 4"; }
else if ($fighter4 == "Brock Lesnar")
{ $sql = "UPDATE Predictions_Poll SET fighter_two_votes = fighter_two_votes + 1 WHERE id = 4"; }
if ($fighter5 == "Nate Diaz")
{ $sql ="UPDATE Predictions_Poll SET fighter_one_votes = fighter_one_votes + 1 WHERE id = 5"; }
else if ($fighter5 == "Tony Ferguson")
{ $sql = "UPDATE Predictions_Poll SET fighter_two_votes = fighter_two_votes + 1 WHERE id = 5"; }
/****************** UPDATE SUCCESS CHECK *******************/
if($conn->query($sql)== true)
{echo "Record Successfully Updated";}
else
{echo "Error Updating Record: " . $conn->error;}
$conn->close();
?>
的属性的对象列表,我试图结合这些值来为查询创建一个片段,例如:
Id
问题是我得到:UNION ALL SELECT 30 UNION ALL SELECT 31 UNION ALL SELECT ...
老实说,我找不到直接从30 UNION ALL SELECT 31 UNION ALL SELECT
获取它的方法,这是我的实现:
string.JOIN
答案 0 :(得分:1)
string.Concat(teams.Skip(1).Select(c => " UNION ALL SELECT " + c.Id));
或避免使用空格:
string.Join(" ", teams.Skip(1).Select(c => "UNION ALL SELECT " + c.Id));
答案 1 :(得分:1)
您可以使用linq。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
public static void Main()
{
var values = new List<int> { 1, 2, 3, 4 };
var str = values
.Select(i => $"UNION ALL SELECT {i} ")
.Aggregate(new StringBuilder(), (sb, a) => sb.Append(a), s => s.ToString());
Console.WriteLine(str);
}
}