早上好,每个人......
我想将两个php代码合并为一个,以减少php多个查询。
我的第一个PHP脚本是这样的:
<?php
include('wp-config.php');
$q = "SELECT CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) FROM table1 ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($q);
$data = mysql_fetch_array($result);
echo $data[0];
?>
上面代码的输出如下:
a1a2a3 x a x a4a5a6
我的第二个PHP脚本与第一个PHP脚本相同,只是它从table2
查询:
<?php
include('wp-config.php');
// this is different vvvvvv //
$q = "SELECT CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) FROM table2 ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($q);
$data = mysql_fetch_array($result);
echo $data[0];
?>
,输出类似:
a1a2a3 x a x a4a5a6
现在我想把上面两个相似的php脚本合并为一个,我不知道怎么样,结果应该如下所示...
Name1 (any text)
a1a2a3 x a x a4a5a6
Name2 (any text)
a1a2a3 x a x a4a5a6
任何人都可以帮我组合以上两个脚本 感谢
答案 0 :(得分:0)
我认为你正在寻找&#34; UNION ALL&#34; :
$q = "SELECT
CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6)
FROM
table1
UNION ALL
SELECT
CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6)
FROM
table2
ORDER BY id DESC LIMIT 1"
这会将两个查询的结果合并为一个结果集
答案 1 :(得分:0)
如果您想订购,请按以下方式使用:
SELECT yourColumn FROM (
SELECT CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) yourColumn FROM table1
UNION ALL
SELECT CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) yourColumn FROM table2) newSet
ORDER BY yourColumn
没有订单:
SELECT CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) yourColumn FROM table1
UNION ALL
SELECT CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) yourColumn FROM table2
或者您可以订购,也可以选择id列
SELECT id, CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) yourColumn FROM table1
UNION ALL
SELECT id, CONCAT(a1,a2,a3,' x ',a,' x ',a4,a5,a6) yourColumn FROM table2
ORDER BY id