[英]MySQL JOIN on different column names
I have two tables with similar data but entirely different column names. 我有两个表,它们的数据相似,但列名却完全不同。 I make the query in a PHP page and then use the data in echo statements like this:
我在PHP页面中进行查询,然后在echo语句中使用数据,如下所示:
<?php echo($row['bedrooms']); ?>
The current query looks like this: 当前查询如下:
$sql_query = "SELECT mls_number, city, list_price, bedrooms
FROM bonita_property_res
WHERE city = "Bonita" AND list_price >= 1500000
ORDER BY list_price DESC";
How do a join a table called naples_property_res that looks like this and still be able to use the php echo as its configured? 如何联接一个名为naples_property_res的表,该表看起来像这样并且仍然能够使用php echo作为其配置?
MLSNumber City ListPrice TotalBeds
--------------------------------------------------
898989 | Bonita | 200000 | 4
It doesn't sound like you want to use a JOIN
, but rather a UNION
听起来好像您不想使用
JOIN
,而是UNION
SELECT fields FROM bonita_property_res WHERE conditions
UNION SELECT fields FROM naples_property_res WHERE conditions
Use UNION: 使用UNION:
SELECT mls_number, city, list_price, bedrooms FROM bonita_property_res WHERE ...
UNION
SELECT MLSNumber AS mls_number, City AS city, ListPrice AS list_price, TotalBeds AS bedrooms FROM naples_property_res WHERE ...
The column aliases - something AS something_else - ensure that you don't break any references in PHP, eg $row['bedrooms']. 列的别名-something AS something_else-确保您不会破坏PHP中的任何引用,例如$ row ['bedrooms']。
Well you didn't tell us what the "entirely different column names" are but it would look something like this: 好吧,您没有告诉我们“完全不同的列名称”是什么,但是它看起来像这样:
SELECT mls_number, city, list_price, bedrooms
FROM bonita_property_res
WHERE city = "Bonita" AND list_price >= 1500000
ORDER BY list_price DESC
UNION
SELECT entirely, different, column, names
FROM naples_property_res
WHERE ......
You can just use ALIAS
to the columns. 您可以只对列使用
ALIAS
。
$sql_query = "SELECT colA AS mls_number, colB AS city, colC AS list_price,
colD AS bedrooms FROM naples_property_res WHERE ...";
And make a UNION
. 并创建一个
UNION
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.