简体   繁体   English

PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id

[英]PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id

Im trying to put together a script that allows me to display links dynamically on my site.我试图组合一个脚本,允许我在我的网站上动态显示链接。

I have 2 tables.我有 2 张桌子。 The first one, domains can have two values per row.第一个, domains每行可以有两个值。 These being, domId and domain .这些是domIddomain The next table, links can contain a few values per row, the ones im working with here are domId (matches the domId of the domains table) desc and `link'.下一个表, links每行可以包含几个值,我在这里使用的是domId (匹配domains表的 domId) desc和“链接”。

Here is my query so far:到目前为止,这是我的查询:

$linkQuery2 = 'SELECT `link`,`desc`,`domId` 
                 FROM `links` 
                WHERE `catId`="'.$pageCat.'" 
                   && (`modId`="1" || modId="'.$pageModel.'") 
             ORDER BY `domainId` ASC 
                LIMIT ' . $from . ', ' . $max_results2;    

Here is the code that im using to display the info i need:这是我用来显示我需要的信息的代码:

$linkLoop2 = '';
$linkAd = $row['link'];
$linkDesc = stripslashes($row['desc']);
$linkDomain = stripslashes($row['id']);
$linkLoop = '<ul class="bymodel-dllinks">';
$linkLoop2 .= '<li><a href="'.$linkAd.'" target="_tab">'.$linkDesc.' '.$linkDomain.'</a></li>';
$linkLoop3 = '</ul>';

What I am trying to do is find a way so that when $linkDomain is echo'd it will display the text i have stored in the domain field on the domains table.我正在尝试做的是找到一种方法,以便在回显 $linkDomain 时,它将显示我存储在domains表的domain字段中的文本。 Currently when $linkDomain is echo'd, it will display 303, which is the domId of the site i will be linking to.目前,当$linkDomain被回显时,它将显示 303,这是我将链接到的站点的domId

Use Join to select domain name from domainstable:使用从 domaintable 加入 select 域名:

$linkQuery2 = 'SELECT l.link,l.desc,l.domId,d.domain
             FROM links l LEFT JOIN domains d ON d.domid = l.domid
            WHERE l.catId="'.$pageCat.'" 
               && (l.modId="1" || l.modId="'.$pageModel.'") 
         ORDER BY l.domainId ASC 
            LIMIT ' . $from . ', ' . $max_results2;

Add the domain column to your select statement.domain列添加到您的 select 语句中。

$linkQuery2 = 'SELECT `link`,`desc`,`domId`,`domain`
             FROM `links` 
            WHERE `catId`="'.$pageCat.'" 
               && (`modId`="1" || modId="'.$pageModel.'") 
         ORDER BY `domainId` ASC 
            LIMIT ' . $from . ', ' . $max_results2;

Change $linkDomain to$linkDomain更改为

$linkDomain = stripslashes($row['domain']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? PHP MYSQL - 突出显示数据库表行IF table1.id存在于table2中 - PHP MYSQL - Highlight a database table row IF table1.id exists in table2 Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id 通过在Table1.id上应用groupBy,但在Table2上没有软删除的行,使用Laravel查询生成器来计算总和? - Calculate sum using Laravel Query Builder by applying groupBy on Table1.id but without soft deleted rows on Table2? Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM