简体   繁体   English

将两个PHP sql语句合并为一个

[英]Combining two PHP sql statements into one

I'm after a little help in the quest for cleaner code... 我在寻求更简洁的代码方面有所帮助...

Current code which is working fine. 当前代码工作正常。 Wondering if I can make it into one SQL statement as opposed to two... 想知道我是否可以将它变成一个SQL语句而不是两个...

$sql = "INSERT INTO table_a (1,2,3,4) VALUES ('$1','$2','$3','$4');";

$result = mysql_query($sql,$mysql_link);

$id = mysql_insert_id();

$sql2 = "INSERT INTO table_b (1,2,3,4) VALUES ('$id','$5','$6','$7');";

$result2 = mysql_query($sql2,$mysql_link);

How can I combine these two to work within my current php script? 我如何结合这两个在我当前的php脚本中工作?

Thanks! 谢谢!

An insert in two different tables is not possible. 无法在两个不同的表中插入。 If you want to reduce your query count you may have to reconsider your database structure. 如果要减少查询数量,则可能必须重新考虑数据库结构。

如上所述,您无法将它们组合在一起,因为插入位于2个不同的表中,尽管您可以编写包含两个查询的存储过程(带有必要的参数)并在PHP中调用该过程,而不用编写这些语句...这将有助于告诉您您要执行此操作的原因,因为我无法理解您是否想要获得更紧凑(可重用)的代码,或者提高数据库的性能...

When I see another question of this kind, I am always wondering, why noone asks how to combine ALL sql queries of the script into one. 当我看到另一种此类问题时,我总是在想,为什么没人问如何将脚本的所有sql查询合并为一个。 All SELECTs, INSERTS, UPDATES. 所有选择,插入,更新。 Wouldn't it be logically? 这不是合乎逻辑的吗?

What's the strange desire to combine? 合并的奇怪愿望是什么? What's the point in it? 有什么意义呢? What's wrong in 2 separate queries? 2个单独的查询出了什么问题?

When you eat, do you mix a salad, a soup, a main dish, a drink into one bowl and then consume it? 吃饭时,您是否将沙拉,汤,主菜,饮料混入一个碗中,然后食用? No? 没有? Why do you want to put all the queries into same bowl then? 那为什么要把所有查询放到一个碗里呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM