简体   繁体   English

如何对所有两个SELECT语句进行UNION?

[英]How to UNION ALL two SELECT statements?

I have 2 tables, one looks like this: 我有2张桌子,一张看起来像这样:

TABLE ONE 表一

id | Last Name | First Name | Username | Password | Secret Question

and another that looks like this: 另一个看起来像这样:

TABLE TWO 表二

id | Hobby | Country | 

I want to combine a Select statement that grabs data from both tables and output the results. 我想组合一个从两个表中获取数据并输出结果的Select语句。 The following code: 如下代码:

$select = mysql_query("

    SELECT * FROM table_one WHERE Username = 'Bob'

    UNION ALL

    SELECT * FROM table_two WHERE Hobby = 'Baseball'

");

while ($return = mysql_fetch_assoc($select)) {

$userName = $return['Username'];

$hobby = $return['Hobby'];

}

echo "$userName likes $hobby";

results in a The used SELECT statements have a different number of columns error, what am I doing wrong? 导致使用的SELECT语句具有不同的列数错误,我在做什么错呢?


EDIT: 编辑:

Running this gives the following error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/studentw/public_html/foo.php on line 16 运行此命令将产生以下错误: 警告:mysql_fetch_assoc():提供的参数不是第16行/home/studentw/public_html/foo.php中的有效MySQL结果资源。

$select = mysql_query("

SELECT FROM TABLE_ONE t1
  INNER JOIN TABLE_TWO t2
    ON t1.id = t2.id
WHERE t1.Username = 'Bob'
  AND t2.Hobby = 'Baseball'

");

while ($return = mysql_fetch_assoc($select)) {

    $firstName = $return['Username'];
    $hobby = $return['Hobby'];

}

echo "$firstName likes $hobby";

The values in the tables are: 表中的值是:

TABLE ONE 表一

id | Last Name | First Name | Username | Password | Secret Question

 1 | Hughes | Bobby | Bob | 123 | Maiden name?

TABLE TWO 表二

id | Hobby | Country | 

 1 | Baseball | USA

I think you want a JOIN , not a UNION or UNION ALL. 我想您想要一个JOIN ,而不是UNION或UNION ALL。

SELECT FROM TABLE_ONE t1
  INNER JOIN TABLE_TWO t2
    ON t1.id = t2.id
WHERE t1.Username = 'Bob'
  AND t2.Hobby = 'Baseball'

Unions require that the data columns of each table be the same number and type, and basically give you a concatenation of multiple rows from different tables. 并集要求每个表的数据列必须具有相同的编号和类型,并且基本上为您提供了来自不同表的多个行的串联。 Joins, on the other hand, essentially expand one table into a wider one with more columns by literally joining another table's columns to it. 另一方面,通过从字面上将另一表的列连接到表,联接实际上将一个表扩展为具有更多列的更宽的表。

When you do a join, you need to specify how the rows of one table correspond to the other; 进行联接时,需要指定一个表中的行与另一个表中的行对应的方式。 in this case, I'm assuming that your Id field is supposed to be a primary/foreign key linking the tables together. 在这种情况下,我假设您的Id字段应该是将表链接在一起的主键/外键。 Apologies if that is incorrect-- if that is so, I will need more information in order to properly help you. 抱歉,如果不正确,那么请您提供更多信息,以帮助您。


Per your edited question: 根据您编辑的问题:

I'll first link you to the documentation for the mysql_query function . 我首先将您链接到mysql_query函数文档 It has one required and one optional argument; 它有一个必选参数和一个可选参数。 the second argument is your connection handle. 第二个参数是您的连接句柄。 If you don't specify it, then PHP assumes that the last connection opened with mysql_connect is the one you want to use. 如果未指定,则PHP会假定您要使用的最后一个使用mysql_connect打开的mysql_connect So my first question is, did you call mysql_connect properly and did that call work successfully? 所以我的第一个问题是,您是否正确调用了mysql_connect ,并且该调用成功完成了吗?

If you're sure the mysql_connect call worked, then I'm not sure what the problem could be. 如果您确定mysql_connect调用有效,那么我不确定是什么问题。 I don't think it could hurt, though, to assign the result of mysql_connect to a variable so you can explicitly specify the connection in mysql_query . 不过,我认为将mysql_connect的结果分配给一个变量不会有什么坏处,因此您可以在mysql_query显式指定连接。 Maybe something like the following: 可能类似于以下内容:

$conn = mysql_connect("localhost:3306", "username", "password");
$query = "select * from some_table"; // obviously use your own query here
$result = mysql_query($query, $conn);

Let me know if that doesn't work. 让我知道那是否行不通。

Yes, Platinum Azure is correct, what you need is a join. 是的,Platinum Azure是正确的,您需要的是联接。 However, your table design needs some help. 但是,您的表设计需要一些帮助。

You need to relate the tables to each other. 您需要将表相互关联。 Easy way to do this is: 简单的方法是:

1) Add a column to table 1: "hobby_id" 2) Then each record record in table 1 needs to have the id from table 2 in the field "hobby_id" 1)在表1中添加一列:“ hobby_id” 2)然后,表1中的每个记录记录都必须在“ hobby_id”字段中具有表2中的ID

Table 1: 表格1:

id | Last Name | First Name | Username | Password | Secret Question | hobby_id
1 | Hughes | Bobby | Bob | 123 | Maiden name? | 2
2 | Smith | Mike | Msmith | mypass | usual | 1

Table 2: 表2:

id | Hobby | Country | 
1 | Baseball | USA
2 | Hockey | Canada
3 | Horse Racing | Japan

See how hobby_id relates to id in table 2? 请参阅表2中的hobby_id与ID如何关联? So, Table 1, Bobby, plays Hockey and Mike plays Baseball. 因此,表1中,鲍比(Bobby)踢曲棍球,麦克(Mike)踢棒球。 No one matches horse racing. 没有人能匹敌赛马。

So to query these tables, it would look like this: 所以要查询这些表,它看起来像这样:

select table1.`last name`,table1.`first name`, table2.`hobby`  from table1
inner join table2 on table1.hobby_id = table2.id

The result would be: 结果将是:

Hughes | Bobby | Hockey
Smith | Mike | Baseball

and your php code then looks like this: 然后您的php代码如下所示:

while ($return = mysql_fetch_assoc($select)) {

    echo $return['first name']." ".$return['last name']." likes ".$return['hobby']."<br/>\n";

}

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

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