gpt4 book ai didi

mysql - 如何组合 2 个 SQL 查询以获得 1 个结果

转载 作者:行者123 更新时间:2023-11-29 19:15:51 25 4
gpt4 key购买 nike

SELECT users.name, users.registrationdate, users.city, users.status
FROM users, referrals
WHERE users.username= referrals.referrals AND referrals.username='user_name'

SELECT adearning, reffearning
FROM earnings WHERE usersname='user_name33'

如何组合这两个查询,就好像第一个查询条件不匹配并且不运行一样。那么第二个查询应该运行并在条件匹配时返回一些值

最佳答案

试试这个

$query1 = $this->db->query("SELECT users.name, users.registrationdate, users.city, users.status 
FROM users, referrals
WHERE users.username = referrals.referrals AND referrals.username='user_name'")
$result1 = $query1->result_array();


$query2 = $this->db->query("SELECT adearning, reffearning FROM earnings WHERE usersname='user_name33' ")
$result2 = $query2->result_array();


$dataArray = array_merge($result1, $result2);

如果想检查结果,请在行尾添加此内容

print_r($dataArray);die;

On this it will print all of them in one array

<小时/>

示例

$result1 = array(
'0' => 'sjhccsd',
'1' => 'ddd',
'2' => 'df',
'3' => 'dfd',
);

$result2 = array(
'0' => 'sjhccsd',
'1' => 'ddd',
'2' => 'df',
'3' => 'dfd',
);


$dataArray = array_merge($result1, $result2);

print_r($dataArray);

输出:数组 ( [0] => sjhccsd [1] => ddd [2] => df [3] => dfd [4] => sjhccsd [5] => ddd [6] => df [7] => dfd )

关于mysql - 如何组合 2 个 SQL 查询以获得 1 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com