从2个表中获取具有$ pdo的相同ID的数据

时间:2022-06-25 16:58:27

I'm trying to get some dataa from a table, and search for another field on other table by the id i got on my first query

我正试图从表中获取一些数据,并通过我在第一个查询中获得的id在其他表上搜索另一个字段

$db = new PDO('mysql:host=localhost;dbname=xxxxx;charset=utf8', 'xxxxx', 'xxxx');
foreach($db->query('SELECT * FROM oc_store') as $row) {
    echo $row['name'].' '.$row['url']. ' '. $row['store_id']; 
}

This works fine. But i have to

这很好用。但我不得不

select value from oc_setting where store_id == oc_store.store_id and key == config_logo

and echo all togheter, like:

和所有的回声一样,如:

echo $row['name'].' '.$row['url']. ' '. $row['store_id']. ' ' .$row['value'];

I tried nested foeach (silly me :P) and also with the LEFT JOIN, but i'm afraid i couldn't get it working, maybe i'm missing the right sintax for that.... Any helps? Thank you all

我尝试了嵌套的foeach(愚蠢的我:P)以及LEFT JOIN,但我担心我无法让它工作,也许我错过了正确的sintax ....有什么帮助吗?谢谢你们

2 个解决方案

#1


0  

you probably mean you need

你可能意味着你需要

select se.value, st.name, st.url
FROM
oc_setting  se
INNER JOIN
oc_store st
ON st.store_id = se.store_id and st.key = 'config_logo'

#2


0  

It didn't work with left join?

它不适用于左连接?

I'm genuinely new here and I'd love to give it a shot.

我真的是新来的,我很乐意尝试一下。

SELECT st.*, se.value FROM oc_store st LEFT JOIN oc_setting se ON st.store_id = se.store_id AND se.key = config_logo;

That's it IMO. Hope it works :)

这就是IMO。希望它有效:)

NOTE: I'm not sure about key = config_logo as where is config_logo is coming from or where it belongs from those two tables. Key, I'm assuming it belongs to oc_settings.

注意:我不确定key = config_logo,因为config_logo来自哪里或者它们来自这两个表。键,我假设它属于oc_settings。

#1


0  

you probably mean you need

你可能意味着你需要

select se.value, st.name, st.url
FROM
oc_setting  se
INNER JOIN
oc_store st
ON st.store_id = se.store_id and st.key = 'config_logo'

#2


0  

It didn't work with left join?

它不适用于左连接?

I'm genuinely new here and I'd love to give it a shot.

我真的是新来的,我很乐意尝试一下。

SELECT st.*, se.value FROM oc_store st LEFT JOIN oc_setting se ON st.store_id = se.store_id AND se.key = config_logo;

That's it IMO. Hope it works :)

这就是IMO。希望它有效:)

NOTE: I'm not sure about key = config_logo as where is config_logo is coming from or where it belongs from those two tables. Key, I'm assuming it belongs to oc_settings.

注意:我不确定key = config_logo,因为config_logo来自哪里或者它们来自这两个表。键,我假设它属于oc_settings。