[英]MySQL SELECT from table where row = (SELECT val1, val2, val3, … etc.)
I'd like to check if row exists, but without telling the column names, but only its values. 我想检查行是否存在,但不告诉列名,而只告诉它值。 IE.
IE浏览器
+------+------+------+
|field1|field2|field3|
+------+------+------+
| abc | cba | val |
+------+------+------+
SELECT * FROM myTable WHERE ... = (SELECT 'abc', 'cba', 'val')
Is it possible? 可能吗?
I'm writing a function in php and I'd like to add a row if such row doesn't exist. 我正在用php写一个函数,如果这种行不存在,我想添加一行。 Function would take
n
parameters, ie. 函数将采用
n
参数,即。
addNewRowIfDoesNotExist('table1', 'abc', 'cba', 'val');
I don't want to modify the singature of function, but i need to check if such row exist in table as told. 我不想修改功能的单一性,但是我需要检查表中是否存在这样的行。 So I'm not passing column names.
所以我没有传递列名。 So how can i check does row exist without telling the col names?
因此,如何在不告知列名的情况下检查行是否存在?
Thanks 谢谢
Mike 麦克风
Thank you for comments. 感谢您的评论。 I've found it on the internet.
我已经在网上找到了。
check if two "select"s are equivalent 检查两个“选择”是否等效
http://www.mysqltutorial.org/mysql-minus/ http://www.mysqltutorial.org/mysql-minus/
Thanks. 谢谢。
Mike 麦克风
If you would like to see what I mean, there you go: 如果您想了解我的意思,请前往:
function insertRecordIntoTableIfNotExist()
{
if (func_num_args() < 2)
exit("Too few arguments at insertRecordIntoTableIfNotExist()<br>");
$str = changeTableIntoStringWithCommasAndQuotes(func_get_args());
$firstComma = strpos($str, ",");
$tableName = substr($str, 0, $firstComma);
$tableName = substr($tableName, 1, strlen($tableName) - 2);
$rest = substr($str, $firstComma + 1);
$result = executeQuery("SELECT " . $rest . " MINUS SELECT * FROM " . $tableName . "_v;");
if ($result != FALSE)
if (hasRows($result))
return;
if (executeQuery("INSERT INTO " . $tableName . " VALUES(NULL, " . $rest . ");") == FALSE)
{
printWrongQuery();
exit("Error while inserting at insertRecordIntoTableIfNotExist()<br>");
}
}
You need to specify a new View before you run this function. 在运行此功能之前,您需要指定一个新的视图。 My tables has at first place
id
, so in view, I don't take it. 我的表格起初有
id
,所以看来,我不接受它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.