douhan1860 2016-08-09 16:13
浏览 31

加入2个sql表

SOLVED: Seems like my server was having issues and was not letting me properly ftp my files. So this whole time my code was correct :/ Just wasted hours of my time! THANKS GO DADDY!

I want to be able to join these two tables on the market_name of table_cars and name of table_price. I want to pull color and market_name of table_cars, and lowest_price and highest_price of table_price. But i only want cars with the market_name like 3 series. Can anyone tell me how to select this?

table_cars
ID     market_name         color             volume
-----------------------------------------------------
 1      BMW M3             alpine white      2
 2      BMW 3 series       blue              4
 3      BMW 4 series       black             4



table_price
ID      name             lowest_price  highest_price   sale_price
--------------------------------------------------------------------
 1      BMW M3           55000         65000         50000
 2      BMW 3 series     35000         42000         30000
 3      BMW 4 series     40000         47000         35000

I have tried the query below but does not seem to be working.

$sql = "SELECT 
table_cars.market_name, 
table_cars.color, 
table_price.lowest_price,
table_price.highest_price
FROM table_cars INNER JOIN table_price 
ON table_cars.market_name=table_price.name 
WHERE market_name LIKE '%3 series%'";

When I echo out the results, nothing appears. Not even "0 results".

$result = $connect->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo
        "<div class='col-lg-4 col-md-6 col-sm-6 col-xs-12'>
          <div class='x_panel tile fixed_height_panel'>
            <div class='x_title name'>
            <h2>" . $row["color"]. "  " . $row["market_name"]. "</h2><br/>
            " . $row["lowest_price"]. " - " . $row["highest_price"]. "
            </div>
          </div>
        </div>";
    }
} else {
    echo "0 results";
} 
  • 写回答

1条回答 默认 最新

  • duanpengya7074 2016-08-09 16:16
    关注

    Could be the the string is not exactly 3 series so try

    $sql = "SELECT 
    table_cars.market_name, 
    table_cars.color, 
    table_price.lowest_price,
    table_price.highest_price
    FROM table_cars INNER JOIN table_price 
    ON table_cars.market_name=table_price.name 
    WHERE market_name LIKE '%3%'
    AND market_name LIKE '%series%'
    ";
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效