0
I’m putting together a list of posts, where besides the post information I need the information of the house that made the post.
I did the following table query posts
and casas
:
SELECT * FROM posts, (SELECT * FROM casas) AS sqlcasas
WHERE casa = $c_id AND casa = sqlcasas.id
but this query is overwriting the column id
table posts
for id
of casa
. For example:
[
{
"id": "9",
"casa": "9",
"midia": "/1564714715.png",
"texto": "...",
"data": "...",
"likes": "26",
"nome": "...",
"endereco": "...",
"cidade": "...",
"telefone": "...",
"logo": "/1564714696.png",
"site": "...",
"facebook": "...",
"instagram": "..."
}
]
And in my front-end this endpoint needs to return me both the home id and the post id.
My tables:
DROP TABLE IF EXISTS `casas`;
CREATE TABLE `casas` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`endereco` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`cidade` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`telefone` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`logo` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`site` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`facebook` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`instagram` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
# Dump da tabela posts
# ------------------------------------------------------------
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`casa` int(11) unsigned DEFAULT NULL,
`midia` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`texto` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`data` date NOT NULL,
`likes` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `casa` (`casa`),
CONSTRAINT `post_casa` FOREIGN KEY (`casa`) REFERENCES `casas` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I tried this query, but returned an error in my mysqli_query()
.
SELECT id AS id_post, casa FROM posts (SELECT id AS id_casa FROM casas) WHERE casa = id_casa
The first thing is to take these asterisks there, and use the name of the fields, and then aliases to differentiate. Example: SELECT
id AS id_post, texto, campo... FROM posts (SELECT id AS id_casa, endereco, cidade) WHERE ...
so you’ll have the Ids separately asid_post
andid_casa
. In addition, it will only bring the really necessary fields and if one day you add or remove a field from the table, maintenance is much easier.– Bacco
I did this and returned the following error: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, bool Given in [...]
– Luan
I had actually seen this question that you marked as duplicate but as her solution did not solve for me I opened this.
– Luan
Unfortunately, if it did not solve, it was a lack of applying properly, because the regular way is to use the same Alias. About the error of
mysqli_fetch_assoc();
is syntax error in the use of the function, nothing related to SQL. However, you can leave comment with more details.– Bacco
So that’s why I opened a new question because it wasn’t the same as mine, even though it was on the same subject. And Murilo’s answer has already helped me and I’ve already marked it as the solution to my question! :)
– Luan