简体   繁体   English

PHP / MySQL联合有两个选择

[英]PHP/MySQL Union with Two Selects

What's wrong with my statement? 我的陈述出了什么问题? I've tried everything I can think of. 我已经尝试了所有我能想到的。

$stmt = $mysqli->prepare("SELECT host_name, review_title FROM lhr_reviews
              UNION
              SELECT host_url FROM lhr_hostinfo WHERE host_name = ?
              ORDER BY host_name");

$stmt->bind_param("s", $id);
$stmt->execute(); 
$res = $stmt->get_result();

while($row = $res->fetch_assoc()) {
  $list .=  "<li><a href='".$row['host_url']."'>".substr($row['review_title'],0,20)."</a></li>";
}

The columns that are selected from the two queries must match each others, in the number of columns and in the corresponding data types: 从两个查询中选择的列必须在列数和相应的数据类型上彼此匹配:

The column names from the first SELECT statement are used as the column names for the results returned. 第一个SELECT语句中的列名用作返回结果的列名。 Selected columns listed in corresponding positions of each SELECT statement should have the same data type. 每个SELECT语句的相应位置中列出的所选列应具有相同的数据类型。 (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.) (例如,第一条语句选择的第一列应与其他语句选择的第一列具有相同的类型。)

In your query, the first select selects two columns, where the second one selects only one column. 在查询中,第一个选择选择两列,第二个选择仅选择一列。 This is not true, you can select empty string in the second select as a work around. 这不是正确的,您可以在第二个选择中选择一个空字符串来解决。 The other issue is the ORDER BY host_name you can add it this way, you have to put these queries inside a subquery and order in the outer one like this: 另一个问题是您可以通过这种方式添加ORDER BY host_name ,您必须将这些查询放入子查询中,并在外部查询中进行排序,如下所示:

SELECT *
FROM
(
    SELECT host_name, review_title FROM lhr_reviews
    UNION
    SELECT host_url, ''            FROM lhr_hostinfo WHERE host_name = ?
) AS sub
ORDER BY host_name;

you shoud try this query.. 您应该尝试此查询。

SELECT host_name, review_title,host_url FROM lhr_reviews
LEFT JOIIN lhr_hostinfo
    on(host_name = ?)
ORDER BY host_name

Problem in UNION . UNION问题。 Selected columns listed in corresponding positions of each SELECT statement should have the same data type.

Try this: 尝试这个:

$stmt = $mysqli->prepare(
             "SELECT * FROM
              (SELECT host_name, review_title FROM lhr_reviews
              UNION
              SELECT host_url, NULL FROM lhr_hostinfo WHERE host_name = ?) A
              ORDER BY host_name");

UNION Syntax UNION语法

Actual problem is inside query because in first query you get two fields and at second query you getting single field and union says column must match including type of column and number of column. 实际问题出在查询内部,因为在第一个查询中您会获得两个字段,而在第二个查询中您将获得单个字段,并会说列必须匹配,包括列类型和列数。

Rules are. 规则是。

Here are some rules that govern the way UNION operator is used in a query. 以下是一些规则,这些规则控制查询中使用UNION运算符的方式。

  1. In a UNION query, there are at least two SELECT statements. 在UNION查询中,至少有两个SELECT语句。

  2. The two SELECT statements must have the same number of columns and the the columns must have compatible data types. 两个SELECT语句必须具有相同数量的列,并且这些列必须具有兼容的数据类型。

  3. The column headings in each of the SELECT statements do not have to have the same name. 每个SELECT语句中的列标题不必具有相同的名称。 The column headings in the result of a UNION query are always taken from the first SELECT statement. UNION查询结果中的列标题始终取自第一个SELECT语句。

  4. If you want to sort the result set of the UNION operation, you can only put an ORDER BY clause after the last SELECT statement. 如果要对UNION操作的结果集进行排序,则只能在最后一个SELECT语句之后放置ORDER BY子句。 ORDER BY clause can't be specified in any other SELECT statements in the UNION query. 不能在UNION查询的任何其他SELECT语句中指定ORDER BY子句。

  5. The column(s) used in ORDER BY clause can only be taken from the first SELECT statement. ORDER BY子句中使用的列只能从第一个SELECT语句中获取。

  6. If you don't specify an ORDER BY clause in the UNION query, the result set is always sorted by the first column. 如果未在UNION查询中指定ORDER BY子句,则结果集始终按第一列排序。

  7. If you use UNION ALL, the entire result set from the second SELECT statement is appended to the first SELECT statement. 如果使用UNION ALL,则第二条SELECT语句的整个结果集将附加到第一条SELECT语句。 In this case, there could be duplicate records in the unioned result set. 在这种情况下,联合结果集中可能有重复的记录。

  8. If you only use UNION, MySQL removes duplicate rows from the final result set. 如果仅使用UNION,则MySQL将从最终结果集中删除重复的行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM