MySQL JOIN对不同的列名称
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($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?
MLSNumber City ListPrice TotalBeds
--------------------------------------------------
898989 | Bonita | 200000 | 4
我有两个表具有相似的数据,但列名完全不同。 我在PHP页面中进行查询,然后使用echo语句中的数据,如下所示: p>
&lt;?php echo($ row ['bedroom']); ?&gt;
code> pre>
当前查询如下所示: p>
$ sql_query =“SELECT mls_number,city, list_price,bedrooms
来自bonita_property_res
WHERE city =“Bonita”AND list_price&gt; = 1500000
ORDER BY list_price DESC“;
code> pre>
如何加入 一个名为naples_property_res的表看起来像这样,仍然可以使用php echo作为其配置? p>
MLSNumber City ListPrice TotalBeds
--------- -----------------------------------------
898989 | 博尼塔| 200000 | 4
code> pre>
div>
Use 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, e.g. $row['bedrooms'].
It doesn't sound like you want to use a JOIN
, but rather a UNION
SELECT fields FROM bonita_property_res WHERE conditions
UNION SELECT fields FROM naples_property_res WHERE conditions
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.
$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
.