如何将查询结果存储到mysql中的变量中,然后使用另一个查询和echo结果?

时间:2015-08-13 10:59:01

标签: php mysql left-join echo

我有一些复杂的情况。我使用LEFT JOIN加入了多个表现在我想将查询结果存储在变量中,然后我想在另一个SELECT查询中调用此结果以从{{1}获取所需数据} D b。  我尝试了这个,但我失败了。

使用以下查询,我可以在变量mysql

中存储查询结果
@v1

现在在下面的查询中我使用变量来存储我的第一个查询的结果

SET @v1 := (SELECT `opinion_id` FROM `pacra_client_opinion_relations` WHERE `client_id` = 97 LIMIT 1);

当我在 SELECT @v1, r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname FROM og_ratings r LEFT JOIN og_companies c ON r.client_id = c.id LEFT JOIN og_rating_types t ON r.rating_type_id = t.id LEFT JOIN og_actions a ON r.pacra_action = a.id LEFT JOIN og_outlooks o ON r.pacra_outlook = o.id LEFT JOIN og_lterms l ON r.pacra_lterm = l.id LEFT JOIN og_sterms s ON r.pacra_sterm = s.id LEFT JOIN companies_contact co ON c.id = co.companies_id LEFT JOIN pacra_client_opinion_relations pr ON pr.opinion_id = c.id LEFT JOIN pacra_clients pc ON pc.id = pr.client_id LEFT JOIN city ON city.id = pc.head_office_id WHERE r.client_id= @v1 ORDER BY r.id DESC LIMIT 1 中执行此查询时,它会显示我的正确结果

enter image description here

但是当我在我的PHP页面中使用此查询并尝试回显我的表格中的某些字段时,它不会显示任何内容。

PhpMyadmin

我的浏览器中显示空白页面。你能告诉我我错在哪里吗?

2 个答案:

答案 0 :(得分:2)

变量只持续当前的mysql“session”,因此在PHP中执行时,第二个查询被视为新会话,因此未设置变量。

例如,如果在phpmyadmin中我运行:

SET @v1 := (SELECT id FROM `mytable` WHERE field='something' LIMIT 1);
SELECT * FROM `mytable` WHERE id = @v1;

工作正常。

但是,如果我跑:

SET @v1 := (SELECT id FROM `mytable` WHERE field='something' LIMIT 1);

然后

SELECT * FROM `mytable` WHERE id = @v1;

由于未设置@ v1,它将返回0结果。

因此我会尝试:

$sql = "SET @v1 := (SELECT `opinion_id` FROM `pacra_client_opinion_relations` WHERE `client_id` = 97 LIMIT 1);
SELECT @v1, r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r 
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN   companies_contact co
ON c.id = co.companies_id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id= @v1
ORDER BY r.id DESC
LIMIT 1";

$conn->multi_query($sql);
$conn->next_result(); //as the first query we don;t need anything from
$result = $conn->use_result();

答案 1 :(得分:1)

我不知道mysql的SET @语法,但我发现你的代码没有意义:

"SET @v1 := (SELECT `opinion_id` FROM `pacra_client_opinion_relations` WHERE `client_id` = 97 LIMIT 1)";
$sql= " SELECT @v1, r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.[...]

这两个中的第一行是没有的(与评论具有相同的效果)。它是一个未以某种方式分配或执行的字符串。您需要将其分配给变量($sql2 = "SET ...")或使用mysql命令执行它