简体   繁体   English

B列表2中的返回值,其中A列表1匹配A列表2

[英]Return Value in Column B Table 2, where column A Table 1, matches Column A Table 2

What I'm trying to do: Where category (projects) matches id (markets) echo category (markets) . 我正在尝试做的事情: 类别 (项目)id (市场)匹配的地方echo 类别 (市场)

Table 1 Sample ( projects table) 表1样本( 项目表)

项目表

Table 2 Sample ( markets table) 表2样本( 市场表)

在此处输入图片说明

Sample of PHP Code PHP代码样本

$num_rec_per_page=5;
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

 function GET($key) {
    return isset($_GET[$key]) ? $_GET[$key] : null;
}

$category= GET('category');

if ($category !== null) {
    $sql = "SELECT * FROM projects WHERE category='$category' LIMIT $start_from, $num_rec_per_page";
} else {
    $sql = "SELECT * FROM projects LIMIT $start_from, $num_rec_per_page";
}

$sql_query = mysql_query($sql);
$post = $sql_query;

$sql1 = "SELECT * FROM markets";
$sql_query1 = mysql_query($sql1);
$marketsinfo = mysql_fetch_array($sql_query1);

In my code below, I've tried putting a while loop within the main while loop since we have to find out what category its in then display it for each blog post. 在下面的代码中,我尝试在主while循环中放置一个while循环,因为我们必须找出其所属的类别,然后为每篇博客文章进行显示。

It only worked for the first result, and then I did some research online and found that it is very poor design to do this. 它只对第一个结果起作用,然后我在网上进行了一些研究,发现这样做是非常糟糕的设计。

Where I'm currently at with displaying the result (see code in between hyphens): 我目前在哪里显示结果(请参见连字符之间的代码):

            <!-- Blog - Start -->
        <?php while ($post = mysql_fetch_array($sql_query)) {?>
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left">
            <div class="row">
                <!-- Blog Image - Start -->
                <div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12  pic inviewport animated delay1" data-effect="fadeIn">
                    <img alt="blog-image" class="img-responsive" src="<?php echo $post['imageoutside'] ?>">
                </div>
                <!-- Blog Image - End -->
                <!-- Blog Info - Start -->
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn">
                    <div class="info">
----------------------------------------------------------------------
                        <span class="date">
                        <?php
                            if($post['category'] === $marketsinfo['id']){
                                echo $marketsinfo['category'];
                            }
                         ?>
                         </span>
----------------------------------------------------------------------
                        <h4 class="title"><a href="projects/readmore.php?job=<?php echo $post['job'] ?>"><?php echo $post['title'] ?></a></h4>
                        <p><?php echo $post['summary'] ?></p>
                        <a class="btn btn-primary text-on-primary"  href="projects/readmore.php?job=<?php echo $post['job'] ?>">Read More</a>
                    </div>
                </div>
                <!-- Blog Info - End -->
            </div>
        </div>
        <?php } ?>
            <!-- Blog - End -->

Hope I've been thorough enough without being confusing. 希望我已经足够彻底,不要感到困惑。 How can a noob accomplish this? 新手如何才能做到这一点? I'll take any pointers you have!! 我会告诉你的!

If you use a join, you can get the category text in one query and avoid all the looping in PHP. 如果使用联接,则可以在一个查询中获得类别文本,并避免PHP中的所有循环。

A LEFT Join will return all records from Projects and only those records which match from markets. 向左联接将返回项目中的所有记录,并且仅返回与市场匹配的那些记录。 So if a projects category doesn't exist in markets, a NULL value will be returned. 因此,如果市场中不存在项目类别,则将返回NULL值。

If you used a INNER JOIN, only records which have corresponding values in BOTH tables would be returned. 如果使用INNER JOIN,则仅返回两个表中具有相应值的记录。

Rule of thumb: get the data you need from the database in 1 trip when you need it. 经验法则:在需要时一次性从数据库中获取所需的数据。 Format in PHP, Datagrab in SQL. 格式为PHP,SQL为Datagrab。 Don't get more than you need, and don't get less. 不要得到比您需要的多的东西,也不要少得到的。

SELECT P.Job, M.ID as Markets_ID, M.Category, P.Title
FROM projects P
LEFT JOIN Markets M
 on P.Category =M.ID 
WHERE P.category='$category'   
LIMIT $start_from, $num_rec_per_page"

Note: you will need to put a table alias on category in the where clause. 注意:您需要在where子句中的类别上放置一个表别名。 I'm assuming your passing in the ID so P.Category was used. 我假设您传递了ID,因此使用了P.Category。

do you want join table projects with tables market by category? 是否要按类别将表项目与表市场一起加入? may be u can do this 也许你可以做到这一点

SELECT p.id as id
     , m.category as category 
  from projects as p 
  left 
  join markets as m 
    on p.category = m.id 
 WHERE m.category='$category' 
 LIMIT $start_from
     , $num_rec_per_page

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

相关问题 如何从表中第3列匹配值而第4列匹配值的表的第4列中获取值? - How to get the value from column 4 in a table where column 3 matches a value and column 4 matches a value ? 如何计算一列与另一张表的一列匹配的行数 - How to COUNT the number of rows where a column matches a column of another table mysql select * from table from table where column a value = column b value laravel - mysql select * from table where column a value = column b value laravel 当第一个表中的列值与第二个表中的列值匹配时,显示一个表中的数据 - Display data from one table when a column value in first table matches column value in second table PHP MySQL:从表A获取行,其中表B的Z列中不存在表A的Z列的值 - PHP MySQL: Getting rows from table A where values of column Z of Table A not present in column Z of table B MYSQL:从表中选择与列中的字符串匹配的地方? - MYSQL: select from table where a string in a column matches? 使用额外的唯一列将A表复制到B表 - copy A table to B table with extra unique column 从表中选择*,其中column =值^ column2 =值 - select * from table where column = value ^ column2= value 在MySQL表的列中查找值并打印返回 - Find value in column of MySQL table and print return 从表中选择,其中列a不等于列b(在同一表上)且tableID = 2 - select from table where column a is not equal column b (on same table) and tableID = 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM