需要帮助创建sql查询MySQLi PHP

时间:2017-07-04 21:44:08

标签: php sql mysqli

我有两张桌子 公司:company_id,company_name 审查:review_id,company_id,review_text

我正在尝试在网站上进行搜索功能。我尝试显示特定公司的所有评论。例如,我有搜索字段,我进入那里" Microsoft"然后我需要关于微软的所有评论。

我尝试进行此类查询,但无效

            $company_name = $_POST["company_name"] ?? '';
            $review_text = $_POST["review_text"] ?? '';
            $searchc = $_POST["searchc"] ?? '';


            if(isset($_POST['show'])){
                $result2 = $mysqli->prepare(
                    "SELECT review_text
                   FROM review
                   WHERE company_id
                   IN (HERE I THINK I NEED EMBED QUERY... BUT NOT SURE..) ?");
                $result2->bind_param("s", $searchc);
                $result2->execute();
                $result2->store_result();
                $row2 = mysqli_fetch_assoc($result2);
            }

            <div class="right">
                <form method="post">
                    <br/>
                    <input name="searchc" type="text" value=""><br/>
                    <input type="submit" value="show" name="show">

                  </form>


                    Text: <?=$row2['review_text']?><br />
            </div>

4 个答案:

答案 0 :(得分:1)

您可以在表上执行SQL连接

$sql = "SELECT review.*, company.company_name
        FROM review
        INNER JOIN company ON review.company_id = company.company_id WHERE 
        company.company_name = ?";
$result2->bind_param("s", $searchc);

答案 1 :(得分:1)

您需要这样的嵌套查询:

$result2 = $mysqli->prepare("SELECT review_text FROM review WHERE company_id 
IN (SELECT company_id from company where company_name=?");
$result2 = bind_param("s", $searchc);
$result2->execute();

答案 2 :(得分:0)

您只需要执行INNER JOIN之类的

SELECT review_text
                   FROM review
                  JOIN  COMPANY on review.company_id = COMPANY.company_id;

好吧,如果您将公司名称作为输入,那么您几乎就是

SELECT review_text
                   FROM review
                   WHERE company_id
                   IN (select company_id from company where company_name = $company_name)

答案 3 :(得分:-1)

试试这个:

SELECT
    REVIEW.*, COMPANY.company_name
FROM
    REVIEW
INNER JOIN COMPANY ON REVIEW.company_id = COMPANY.company_id
WHERE
    COMPANY.company_name = "$search";

$ search =&#39; Apple&#39;:

的结果
"4" "2" "Apple Review 1"    "Apple"
"5" "2" "Apple Review 2"    "Apple"
"6" "2" "Apple Review 3"    "Apple"

试试这个:

SELECT
    REVIEW.*, COMPANY.company_name
FROM
    REVIEW
INNER JOIN COMPANY ON REVIEW.company_id = COMPANY.company_id
WHERE
    COMPANY.company_name = "$search";

$ search =&#39; Apple&#39;:

的结果
"4" "2" "Apple Review 1"    "Apple"
"5" "2" "Apple Review 2"    "Apple"
"6" "2" "Apple Review 3"    "Apple"

<强> EDIT 1: 我使用PostgreSQL,这是我要向您展示的唯一区别,就是当您创建表并插入数据时。执行所需操作的SQL语句是相同的。

首先,创建2个表companyreview

CREATE TABLE company
(
  company_id integer NOT NULL,
  company_name character varying(255) NOT NULL,
  CONSTRAINT company_pkey PRIMARY KEY (company_id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE company
  OWNER TO postgres;

并查看表格:

CREATE TABLE review
(
  review_id integer NOT NULL,
  company_id integer NOT NULL,
  review_text character varying(255),
  CONSTRAINT review_pkey PRIMARY KEY (review_id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE review
  OWNER TO postgres;

创建表后,在其中插入数据。对于您的问题,我使用以下...

Company Table:
-----------------------------
company_id  |   company_name
-----------------------------
|   1       |   Microsoft   |
|   2       |   Apple       |
|   3       |   NASA        |
-----------------------------

并查看表格:

-----------------------------------------------------
review_id   |   company_id  |   review_text         |
----------------------------|------------------------
|   1       |   1           |   Microsoft Review 1  |
|   2       |   1           |   Microsoft Review 2  |
|   3       |   1           |   Microsoft Review 3  |
|   4       |   2           |   Apple Review 1      |
|   5       |   2           |   Apple Review 2      |
|   6       |   2           |   Apple Review 3      |
|   7       |   3           |   NASA Review 1       |
|   8       |   3           |   NASA Review 2       |
|   9       |   3           |   NASA Review 3       |
-----------------------------------------------------

在此之后,您可以使用SQL管理员(Navicat,MySQL Workbench或您使用的任何内容)检查以下SQL语句的输出... 新的SQL查询,你写:

SELECT
    review.*, company.company_name
FROM
    review
INNER JOIN company ON review.company_id = company.company_id
WHERE
    company.company_name = 'Apple';

,输出将是......

---------------------------------------------------------------------
review_id   |   company_id  |   review_text         |   company_name|
---------------------------------------------------------------------
|"4"        |   "2"         |   "Apple Review 1"    |"Apple"        |
|"5"        |   "2"         |   "Apple Review 2"    |"Apple"        |
|"6"        |   "2"         |   "Apple Review 3"    |"Apple"        |
---------------------------------------------------------------------

要在PHP代码中使用该SQL语句,这很简单......

$result2 = $mysqli->prepare("SELECT
    review.*, company.company_name
FROM
    review
INNER JOIN company ON review.company_id = company.company_id
WHERE
    company.company_name = $company_name");

...