MySql error when trying to combine two tables

51
January 12, 2019, at 01:00 AM

Hi i am getting the error below when i am tryin to combine two tables, Post and User

error: extraneous input 'post' expecting {, ';', K_ALTER, K_ANALYZE, > > K_ATTACH, K_BEGIN, K_COMMIT, K_CREATE, K_DELETE, K_DETACH, K_DROP, K_END, K_EXPLAIN, K_INSERT, K_PRAGMA, K_REINDEX, K_RELEASE, K_REPLACE, K_ROLLBACK, K_SAVEPOINT, K_SELECT, K_UPDATE, K_VACUUM, K_VALUES, K_WITH, UNEXPECTED_CHAR}

I have a table that has a number of posts which consists of a title, body and by which user created that post,

The user table is a list of users details such as there names and addresses.

I am trying to create a resultset that outputs the post details such as the title and body, plus the user's username attached to it(the post table only has a user id reference)

This is my query i tried

SELECT post.title AS title, post.body AS body, post.username AS username FROM post, user, WHERE user.id = post.userId

My sql skills are a bit rusty but i believe the above use case query can be done?

Answer 1

It sounds like you want a table join. I assume when you wrote post.username you mean user.username, since you later say that the post table only has a userId reference.

Something like this might be what you want:

SELECT post.title, post.body, user.username
FROM post
INNER JOIN user
ON post.userId=user.id;
Answer 2

you should try something like this:

SELECT p.title AS title,p.body AS body,p.username AS username 
FROM post p 
JOIN user u ON u.id = p.userId
WHERE u.id = 'to your user id'
Answer 3

Your Query looks good but remove the comma before where clause:

from

SELECT post.title AS title, post.body AS body, post.username AS username FROM post, user, WHERE user.id = post.userId

to

SELECT post.title AS title, post.body AS body, post.username AS username FROM post, user WHERE user.id = post.userId
READ ALSO
Table to show the latest results from SQL

Table to show the latest results from SQL

I have a page to increase the tracking on Facebook, I created a table to display the latest results in the id column, but this table shows the oldest results for the highest resultsPlease help me edit the table to show the latest results

53
Xampp connects to MariaDB instead of mysql (KNP/SymfonyCasts tutorial “how to speak database”)

Xampp connects to MariaDB instead of mysql (KNP/SymfonyCasts tutorial “how to speak database”)

I am following tutorial from KNP/SymfonyCasts and I have problem with connecting to mysql

53
Django project, I need advice about my model

Django project, I need advice about my model

I need for my users to be able to save the body and slug fields from my Post model in my database, but I have an issueI have 10 fields named body1, body2 etc

27