[英]Finding a solution to making a MYSQL JOIN work with relational tables and a global variable
EDIT: The following code shows the answer to the question. 编辑:以下代码显示了问题的答案。 thanks go to VolkerK.
感谢去VolkerK。
$result = mysqli_query($link,"SELECT ctas.*, ctas_pr.*, nav.id
FROM web_ctas AS ctas
LEFT JOIN web_ctas_page_relation AS ctas_pr
ON ctas.id = ctas_pr.cta_id
LEFT JOIN web_navbar AS nav
ON ctas_pr.navbar_id = nav.id
AND nav.link_name = '".$page."'")
or die(mysqli_error($link)
);
Good morning/afternoon/evening, 早上/下午/晚上好,
I am hoping some of you can help me out with a specific problem regarding mySQL. 我希望你们中的一些人可以帮助我解决有关mySQL的特定问题。
I am trying to JOIN
two tables pass some logic and get back the relevant field data for a match, I shall have to explain the process and the tables so you can garner a modicum of understanding of the code and processes. 我试图
JOIN
两个表以传递一些逻辑,并取回匹配的相关字段数据,我将不得不解释过程和表,以便您对代码和过程有一点了解。
The "web_ctas" table has a relational field that corresponds with the web_navbar table. “ web_ctas”表具有一个与web_navbar表相对应的关系字段。
Massive thank you in advance to anyone who can help on this matter. 在此先感谢所有可以提供帮助的人。
Below are the two tables: 以下是两个表:
web_ctas web_ctas
id | title | img_src | img_alt | link_href | link_title | desc
==============================================================
1 | title | pears | apples | foo | bar | blah
2 | title | apples | pears | bar | foo | bleh
web_navbar web_navbar
id | link_name | visible
========================
1 | home | 1
2 | products | 1
web_ctas_page_relation web_ctas_page_relation
id | cta_id | navbar_id
=======================
1 | 1 | 2
2 | 2 | 2
Below is the SQL I have thus far (I know this probably wont even work): 下面是到目前为止我拥有的SQL(我知道这可能甚至行不通):
$query = mysqli_query($link,"SELECT web_ctas.*, web_navbar.id AS nav_id
FROM web_ctas
JOIN web_navbar
ON web_navbar.id = web_ctas.page_relation
AND page_relation='".$page."'");
Below is the HTML/PHP which will pull back the data from the SQL query results. 下面是HTML / PHP,它将从SQL查询结果中提取数据。
$page = 'home';
while($row = mysqli_fetch_object($query)): ?>
<li>
<a href="<? echo $row->link_href; ?>" title="<? echo $row->link_title; ?>">
<h3><? echo $row->title; ?></h3>
<div>
<img src="<? echo $row->img_src; ?>" alt"<? echo $row->img_alt; ?>">
</div>
</a>
</li> <?
endwhile;
So in a nutshell what I am looking for is a way in which to get the link_name field from the web_navbar table when it matches a row in web_ctas which has the relational id within the page_relation field and matches the variable $page. 所以,简单地说就是我寻找的是在从web_navbar表得到LINK_NAME场时,它匹配web_ctas一排它具有page_relation领域内的关系ID和变量$页相匹配的方式。
If I have missed any key code/info please let me know. 如果我错过任何关键代码/信息,请告诉我。
Sorry, got to go; 对不起,走了; only time for an example script but not for an explaination.
仅用于示例脚本的时间,而无需解释。 Since you already figured out the relation-table thingy yourself only given the 1NF link I'm confident you will be able to dig through this one as well ;-)
由于您仅在给出1NF链接的情况下自己就已经弄清了关系表,所以我相信您也将能够深入研究这一关系表;-)
Works the same way using mysqli instead of pdo_mysql. 使用mysqli代替pdo_mysql的工作方式相同。
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);
$navbar = 'nav_36'; // this would be $navbar='products'; in your case
$stmt = $pdo->prepare('
SELECT
nv.id as navid,
nv.link_name,
ct.id as ctasid,
ct.title,
ct.link_href
FROM
tmp_web_navbar as nv
LEFT JOIN
tmp_web_ctas_nav_rel as r
ON
nv.id = r.id_navbar
LEFT JOIN
tmp_web_ctas as ct
ON
r.id_ctas=ct.id
WHERE
nv.link_name = ?
');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute(array($navbar));
foreach($stmt as $row ) {
foreach($row as $k=>$v) {
echo $k,'=', $v, ' ';
}
echo "\n";
}
// boilerplate for creating table layout and populating some example data
function setup($pdo) {
$pdo->exec('
CREATE TEMPORARY TABLE tmp_web_navbar (
id int auto_increment,
link_name varchar(32),
visible varchar(32),
primary key(id)
)
');
// add some entries to tmp_web_navbar since the example data uses the range [1,50]
$stmt = $pdo->prepare('INSERT INTO tmp_web_navbar (link_name,visible) VALUES (?,?)');
for($i=1; $i<60; $i++) {
$stmt->execute(array(sprintf('nav_%02d', $i), 'title'));
}
$pdo->exec('
CREATE TEMPORARY TABLE tmp_web_ctas (
id int auto_increment,
title varchar(32),
link_href varchar(32),
primary key(id)
)
');
// add some ctas elements
$stmt = $pdo->prepare('INSERT INTO tmp_web_ctas (title,link_href) VALUES (?,?)');
for($i=1; $i<60; $i++) {
$stmt->execute(array('title', sprintf('ctas_%02d', $i)));
}
$pdo->exec("INSERT INTO tmp_web_ctas (title,link_href) VALUES
('title','foo'),
('title ','bar')
");
// a table to store the relationships
$pdo->exec('
CREATE TEMPORARY TABLE tmp_web_ctas_nav_rel (
id int auto_increment,
id_ctas int,
id_navbar int,
primary key(id),
key(id_ctas)
)
');
// and define some relationships
// three of them having id_navbar=36: cats=4,5 and 7
$stmt = $pdo->prepare('INSERT INTO tmp_web_ctas_nav_rel (id_ctas,id_navbar) VALUES (?,?)');
$rel = array(
1=>array(1,6,10,50),
2=>array(2,13,15,30),
3=>array(9,7,11,51),
4=>array(17,24,36,34), // here
5=>array(18,24,36,35), // here
6=>array(19,24,33,38),
7=>array(19,24,32,36), // and here
8=>array(19,24,39,38),
);
foreach($rel as $ctas=>$navrels) {
foreach( $navrels as $nav ) {
$stmt->execute(array($ctas,$nav));
}
}
}
$query = mysqli_query($link,"SELECT web_ctas.*, web_navbar.id AS nav_id
FROM web_ctas
JOIN web_navbar
ON web_navbar.id IN (web_ctas.page_relation)
AND page_relation='".$page."'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.