[英]combine two phpmysql queries
I want to combine these two php mysql queries and want to get one resultset array. 我想结合这两个php mysql查询,并想要得到一个结果集数组。 first query gives all members and postby related posts and second query gives all admin related posts with
member_id
is 0 and post_by
with 0 and admin_id
is not 0 and super_admin_post
0. 第一个查询给出所有与memberby和postby相关的帖子,第二个查询给出所有与
member_id
为0, post_by
为0且admin_id
不为0和super_admin_post
0的与admin相关的帖子。
Database: 数据库:
1) wall_post_master this my first database fields. 1)wall_post_master这是我的第一个数据库字段。
wp_id
, wp_cat_id
, wl_text
, wp_img
, datetime
, displevel
, mg_id
, member_id
, admin_id
, family_id
, post_by
, photo_id
, post_category
, generation_id
, visible_to
, highlight
, super_admin_post
wp_id
, wp_cat_id
, wl_text
, wp_img
, datetime
, displevel
, mg_id
, member_id
, admin_id
, family_id
, post_by
, photo_id
, post_category
, generation_id
, visible_to
, highlight
, super_admin_post
2) admin_user_master this my second database fields. 2)admin_user_master这是我的第二个数据库字段。
user_id
, family_id
, first_name
, last_name
, password
, email_id
, user_level
user_id
, family_id
, first_name
, last_name
, password
, email_id
, user_level
Here my conditions to show wallpost data: 这是我显示留言墙数据的条件:
1)if mg_id exits with multiple member_id relates to comma. 1)如果mg_id退出,且有多个member_id与逗号相关。
2)if visible_to exits with multiple member_id relates to comma. 2)如果visible_to退出且具有多个member_id与逗号相关。
3)if member_id exits with current member_id. 3)如果member_id与当前member_id一起退出。
4)if member_id exits in post_by. 4)如果member_id在post_by中退出。
5)if member_id is 0 and post_by is 0 and super_admin_post = 0 then i want to display as admin post. 5)如果member_id为0,post_by为0,super_admin_post = 0,那么我想显示为管理员帖子。
First Query: 第一个查询:
$grpsearch = "";
foreach ($arrseltran as $grpid) {
$grpsearch .= " OR ( FIND_IN_SET('" . $grpid . "', wp.mg_id) AND displevel = 3)";
}
if($time == ""){ $time .= " WHERE"; }else{ $time .=" AND"; }
$time .= "
wp.family_id = " . $_SESSION['logft']['family_id'] . " AND
wp.post_category = 0 AND
(
wp.member_id = " . $_SESSION['logft']['member_id'] . " OR
wp.post_by = " . $_SESSION['logft']['member_id'] . " OR
FIND_IN_SET('" . $_SESSION['logft']['member_id'] . "', wp.visible_to) " . $grpsearch . " OR
displevel = 1
)";
$timeline = "SELECT wp.*,
wp.member_id,
wp.datetime AS TimeSpent,
wp_cat.font_color,
photo.photo_path,
mm.first_name,
mm.middle_name,
mm.last_name,
mm1.first_name AS to_first_name,
mm1.middle_name AS to_middle_name,
mm1.last_name AS to_last_name
FROM wall_post_master wp
INNER JOIN
wallpost_cat_master wp_cat ON wp_cat.wp_cat_id = wp.wp_cat_id
INNER JOIN
member_master mm ON mm.member_id = (CASE WHEN (wp.post_by = 0) THEN wp.member_id ELSE wp.post_by END)
INNER JOIN
member_master mm1 ON mm1.member_id = wp.member_id
LEFT JOIN
photo_master photo ON photo.photo_id = mm.photo_id
" . $time ." ORDER BY Timespent DESC";
Second Query: 第二个查询:
$timeline1 = "SELECT wpa.*,
wpa.family_id,
wpa.datetime AS TimeSpent,
wp_cat.font_color,
wpa.wp_img,
c.family_id,
c.family_name,
c.editor_photo,
aum.first_name,
aum.last_name
FROM wall_post_master wpa
INNER JOIN
wallpost_cat_master wp_cat ON wp_cat.wp_cat_id = wpa.wp_cat_id
INNER JOIN
config c ON c.family_id = wpa.family_id
INNER JOIN
admin_user_master aum ON aum.family_id = wpa.family_id
where
aum.user_level = 1 and
wpa.admin_id <> 0 and
wpa.family_id='".$_SESSION['logft']['family_id']."' and
wpa.member_id=0 and
wpa.post_by=0 and
wpa.super_admin_post=0
ORDER
BY Timespent DESC";
使用Join本身的概念
select a.col1,a.col2,b.col1,b.col3 from first_table as a, second_table as b where a.col1=condition and b.col1=condition
you can use mysqli function: 您可以使用mysqli函数:
mysqli_multi_query()
or 要么
mysqli::multi_query()
http://php.net/manual/en/mysqli.multi-query.php http://php.net/manual/en/mysqli.multi-query.php
full example from link: 来自链接的完整示例:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>
Procedural style
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
/* close connection */
mysqli_close($link);
?>
if you match up the names of the fields selected in both queries you caan also use UNION 如果您匹配两个查询中选择的字段名称,则可以使用UNION
http://dev.mysql.com/doc/refman/5.0/en/union.html http://dev.mysql.com/doc/refman/5.0/en/union.html
http://www.mysqltutorial.org/sql-union-mysql.aspx http://www.mysqltutorial.org/sql-union-mysql.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.