热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从2个表中选择并搜索值-selectfrom2tablesandsearchforavalue

Ihavetwotables:我有两张桌子:bookingbookbookingbookborrowbookborrowbookandIneedtose

I have two tables:

我有两张桌子:

  1. bookingbook
  2. bookingbook
  3. borrowbook
  4. borrowbook

and I need to search the column bookId in each table if it's exist or not

如果它存在与否,我需要在每个表中搜索列bookId

My code

我的代码

prepare('SELECT
        a.bookId, b.bookId
        FROM
        bookingbook AS a
        INNER JOIN borrowbook AS b ON (a.bookId = b.bookId)
        WHERE a.bookId=? OR b.bookId=?
        ');
        $checkBorrow->bind_param('ii', $bokId, $bokId);
        $checkBorrow->execute();
        $res = $checkBorrow->get_result();
        $numRows = mysqli_num_rows($res);


        if ($numRows > 0) {
            $states = "Not Available";
        } elseif ($numRows <= 0) {
            $states = "Available";
        }
    }

?>

Result always comes Available even if it's not

结果总是可用即使不是

bookingbook Table
id - insId - bookId - studentId
bookingbook Table
id - insId - bookId - studentId - borrowDate - restoreDate

1 个解决方案

#1


1  

This is what you can do to get the number of rows returned,

这是你可以做的,以获得返回的行数,

while ($book = mysqli_fetch_assoc($results)) {
    $bokId = $book['id'];

    // Prepare query
    $checkBorrow = $db->prepare('SELECT * FROM bookingbook LEFT JOIN borrowbook ON 1=1 WHERE bookingbook.bookId=? OR borrowbook.bookId=? UNION SELECT * FROM bookingbook RIGHT JOIN borrowbook ON 1=1 WHERE bookingbook.bookId=? OR borrowbook.bookId=?');

    // Binds variables to prepared statement
    $checkBorrow->bind_param('iiii', $bokId, $bokId, $bokId, $bokId);

    // Execute query
    $checkBorrow->execute();

    // Store the result (to get properties)
    $checkBorrow->store_result();

    // Get the number of rows
    $numRows = $checkBorrow->num_rows;

    if ($numRows) {
        $states = "Not available";
    } else{
        $states = "Available";
    }

    // free results
    $checkBorrow->free_result();

    // close statement
    $checkBorrow->close();
}

Caution: You're mixing the procedural and object oriented style of mysqli. In the while loop condition you're using procedural style, whereas inside the while loop block you're using object oriented style of mysqli. Please pick one style and stick to it.

警告:你正在混合mysqli的程序和面向对象的风格。在while循环条件中,您使用的是过程样式,而在while循环块中,您使用的是面向对象的mysqli样式。请选择一种风格并坚持下去。

Edited:

编辑:

Updated SQL query

更新了SQL查询

// Prepare query
$checkBorrow = $db->prepare('
SELECT * FROM bookingbook LEFT JOIN borrowbook ON 1=1 WHERE bookingbook.bookId=? OR borrowbook.bookId=? 
UNION 
SELECT * FROM bookingbook RIGHT JOIN borrowbook ON 1=1 WHERE bookingbook.bookId=? OR borrowbook.bookId=?');

// Binds variables to prepared statement
$checkBorrow->bind_param('iiii', $bokId, $bokId, $bokId, $bokId);

推荐阅读
  • OkayIaskedaquestionlastweek.Igotafewanswers,severalwhichdidnothelp;however,Idecid ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • echarts无数据时显示无数据_面试题|无索引如何删除亿级数据?
    作者:杨奇龙标签:MySQL、无索引、删除亿级数据转自:yangyidba(yangyidba)一业务需求某业务表a数据量大约4.7亿&# ... [详细]
  • 事务处理事务基本原理如果不开启事务,执行一条sql,马上会持久化数据。可见:默认的mysql对sql语句的执行是自动提交的!如果开启了事务,就是关闭了自动提交的功能,改成了commit执行 ... [详细]
  • 怎么在PHP中利用mysqli实现一个Model类?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文介绍了在MySQL8.0中如何查看性能并解析SQL执行顺序。首先介绍了查询性能工具的开启方法,然后详细解析了SQL执行顺序中的每个步骤,包括from、on、join、where、group by、having、select distinct、union、order by和limit。同时还介绍了虚拟表的概念和生成过程。通过本文的解析,读者可以更好地理解MySQL8.0中的性能查看和SQL执行顺序。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • MySQL多表数据库操作方法及子查询详解
    本文详细介绍了MySQL数据库的多表操作方法,包括增删改和单表查询,同时还解释了子查询的概念和用法。文章通过示例和步骤说明了如何进行数据的插入、删除和更新操作,以及如何执行单表查询和使用聚合函数进行统计。对于需要对MySQL数据库进行操作的读者来说,本文是一个非常实用的参考资料。 ... [详细]
  • <?php/**菜鸟分享如果代码有错误,忘指点.小编本着交流,学习,分享的目的,希望各位大神多多担待**/$link=mysql_connec ... [详细]
  • 这篇文章给大家分享的是有关PHP5.5基于mysqli连接MySQL数据库和读取数据操作的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起 ... [详细]
author-avatar
郑子宜4262
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有