1
Good afternoon!
In my database, I have two tables:
imoveisvenda
and imoveislocacao
The two have the field "dormitory", which tells how many dormitories have the house or apartment, be it for sale or lease.
I want to create a SELECT
to make it easier for those browsing the site.
I have tried several combinations on Phpmyadmin, but without success.
How can I make this query, to deploy in my PHP page?
The last one I tried was: SELECT * imoveislocacao INNER JOIN imoveisvenda WHERE dormitorios = 3
. Once again, without success!
I’ve looked at a few posts here from Stackoverflow, but I couldn’t deploy.
Can someone help me?
UPDATING:
My friend @Ricardo, I want to thank you for the support. And, as requested, follow the error that was generated below:
Notice in ./libraries/sqlparser.lib.php#2477
Undefined offset: -2
Backtrace
./libraries/sql.lib.php#1456: PMA_SQP_format(
array,
string 'query_only',
integer 0,
integer 2,
)
./libraries/sql.lib.php#1574: PMA_countQueryResults(
integer 3,
boolean false,
boolean false,
string 'adimovel_imoveis',
string '',
array,
array,
)
./libraries/sql.lib.php#2411: PMA_executeTheQuery(
array,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3) LIMIT 0, 25 ',
boolean false,
string 'adimovel_imoveis',
string '',
NULL,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3)',
NULL,
)
./import.php#708: PMA_executeQueryAndSendQueryResponse(
array,
boolean false,
string 'adimovel_imoveis',
string '',
NULL,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3)',
NULL,
boolean false,
NULL,
NULL,
NULL,
array,
string 'db_structure.php',
string './themes/pmahomme/img/',
NULL,
NULL,
NULL,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3)',
NULL,
NULL,
)
Notice in ./libraries/sqlparser.lib.php#2482
Undefined offset: -1
Backtrace
./libraries/sql.lib.php#1456: PMA_SQP_format(
array,
string 'query_only',
integer 0,
integer 2,
)
./libraries/sql.lib.php#1574: PMA_countQueryResults(
integer 3,
boolean false,
boolean false,
string 'adimovel_imoveis',
string '',
array,
array,
)
./libraries/sql.lib.php#2411: PMA_executeTheQuery(
array,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3) LIMIT 0, 25 ',
boolean false,
string 'adimovel_imoveis',
string '',
NULL,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3)',
NULL,
)
./import.php#708: PMA_executeQueryAndSendQueryResponse(
array,
boolean false,
string 'adimovel_imoveis',
string '',
NULL,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3)',
NULL,
boolean false,
NULL,
NULL,
NULL,
array,
string 'db_structure.php',
string './themes/pmahomme/img/',
NULL,
NULL,
NULL,
string '(SELECT iv.id, iv.dormitorio FROM imoveisvenda iv WHERE iv.dormitorio = 3) UNION (SELECT il.id, il.dormitorio FROM imoveislocacao il WHERE il.dormitorio = 3)',
NULL,
NULL,
)
Finally, my complete code was as follows:
<?php
$hostdb = "**********";
$userdb = "**********";
$passdb = "**********";
$tabledb = "**********";
$conecta = mysql_connect($hostdb, $userdb, $passdb) or die (mysql_error());
@mysql_select_db($tabledb, $conecta) or die ("Erro ao conectar com o banco de dados");
$busca_query = mysql_query("SELECT * FROM imoveisvenda WHERE imoveisvenda.dormitorio = 3 UNION ALL SELECT * FROM imoveislocacao WHERE imoveislocacao.dormitorio = 3")or die(mysql_error());
if (empty($busca_query)) {
echo "Nenhum registro encontrado.";
}
while ($dados = mysql_fetch_array($busca_query)) { ?>
<img src="<?php echo "$dados[bancoimgthumb]";?>" class="img-responsive"> <?php
echo "Imóvel: $dados[imovel]<br />";
echo "Localização: $dados[localizacao]<br />"; ?>
Mais detalhes: <a href="<?php echo "$dados[file]";?>?id=<?php echo "$dados[id]";?>">Clique aqui!</a><br />
<?php echo "<hr>";
}
?>
EVERYTHING WORKING PERFECTLY! Thank you to everyone who has given up their time to help me! Hugs!
What is the table id
imoveisvenda
andimoveislocacao
?– gato
It is the same "id" field, same for both.
– Hebert Richard
Try it on there:
SELCT * FROM imoveisvenda iv INNER JOIN imoveisalocacao ia ON (iv.id = ia.id) WHERE iv.dormitorios = 2 AND ia.dormitorios = 2
– gato
I’ll try, I’ll be right back.
– Hebert Richard
1054 - Unknown column 'iv.dormitorios' in 'Where clause'
– Hebert Richard
Puts your field
dormitorios
in the clauseWHERE
, I don’t know how he’s appointed in his bank.– gato
It is dorm, I corrected, I had not noticed... but the query does not return anything, nor error message. Very strange!
– Hebert Richard
It is probably because there should be no domitorio with value 2
– gato
I thought that too. I changed to 3 and 1, nothing...
– Hebert Richard
I tried so:
SELECT dormitorio FROM imoveislocacao INNER JOIN imoveisvenda ON dormitorio = id WHERE dormitorio = 3
and the result was:Column 'dormitorio' in field list is ambiguous
– Hebert Richard
From a look here: http://answall.com/questions/6441/qual%C3%A9-a-difference%C3%A7a-entre-Inner-Join-e-outer-Join/6448#6448
– gato
@Denercarvalho, I will take a look. I will return with the solution, no doubt! Hug, friend!
– Hebert Richard