dotibrb048760 2018-08-15 10:32
浏览 79
已采纳

两个不相关的表,具有匹配和反对功能

How do I combine two unrelated tables into one SQL select statement request. However, both tables need to have the Match and Against functions for full text search. I'm getting a blank response and when I do a single table SQL match and against request it works fine but not when I do two tables.

Table One: transport - only id is a primary key integer auto_increment but the rest are varchar

    +----+---------+-----------+--------------+
    | id | title   | type      | tags         |
    +----+---------+-----------+--------------+
    | 1  | triumph | motorbike | sport, black |
    +----+---------+-----------+--------------+
    | 2  | bmw     | car       | hatchback    |
    +----+---------+-----------+--------------+

Table Two: automobile - - only id is a primary key integer auto_increment but the rest are varchar

    +----+-----------+-----------+------------+---------+
    | id | name      | kind      | link       | listed  |
    +----+-----------+-----------+------------+---------+
    | 1  | suzuki    | motorbike | /bike/new/ | green   |
    +----+-----------+-----------+------------+---------+
    | 2  | volkwagan | car       | /car/new/  | limited |
    +----+-----------+-----------+------------+---------+

I need it to print out something like this (just a note, not sure how I would need id - perhaps two columns id_automobile and id_transport to references both above tables)

    +----+-----------+-----------+------------+--------------+
    | id | title     | type      | link       | tags         |
    +----+-----------+-----------+------------+--------------+
    | 1  | suzuki    | motorbike | /bike/new/ | green        |
    +----+-----------+-----------+------------+--------------+
    | 2  | triumph   | motorbike |            | sport, black |
    +----+-----------+-----------+------------+--------------+
    | 3  | bmw       | car       |            | hatchback    |
    +----+-----------+-----------+------------+--------------+
    | 4  | volkwagan | car       | /car/new/  | limited      |
    +----+-----------+-----------+------------+--------------+

My failed attempt:

    <table>
      <tr>
        <th>Title</th>
        <th>Type</th>
        <th>Link</th>
        <th>Tags</th>
      </tr>

    if(isset($_GET['search'])) {
        $search = $_GET['search'];
    } else {
        $search = '';
    }

    $sql = "SELECT * FROM `transport` WHERE MATCH(title, tags) AGAINST('".$search."') CROSS JOIN `automobile` WHERE MATCH(name, listed) AGAINST('".$search."')";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>".$row["title"]."</td><td>".$row["type"]."</td><td>".$row["link"]."</td><td>".$row["tags"]."</td>";
            echo "</tr>";
        }
    } else {
        echo "0 results";
    }
    $conn->close();


    </table>

I would appreciate any help please

  • 写回答

1条回答 默认 最新

  • dputlf5431 2018-08-15 10:52
    关注

    A UNION clause should give you the results that you need.

    Just make sure that bost SELECTs, combined with UNION, have similar column sets. You can use AS to define aliases.

    Something like this:

    SELECT title, type, '' AS link, tags
    WHERE MATCH(title, tags) AGAINST('...')
    FROM transport
    UNION
    SELECT name AS title, kind AS type, link, listed AS tags
    FROM automobile
    WHERE MATCH(title, kind) AGAINST('...')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据