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?
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;
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'
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
Related
I am trying to get all users which don't have addresses, there is a relation between user and address tables(one to many) and the address has an owner attribute that refers to Users pk. how can I achieve that ??
SELECT {u:pk}
FROM {User AS u
LEFT JOIN Address AS a ON {u:pk}={a:owner}}
WHERE {u:owner} IS NULL
I wrote a query which I guess doesn't work correctly
You can try something like this :
SELECT {pk} FROM {User} WHERE {pk} not in ({{ SELECT {owner} FROM {Address} WHERE {owner} IS NOT NULL }})
I have 4 tables in my DB. User , user_details, tags and taggables. By using the user table I am getting user along with user detail and tags. Following is table schema
Table user:
id, name, email, password
Table user_details
user_id, about, vision, picture
Table tags:
id, name
Table taggables
user_id, tag_id
here is my query:
User::with('userDetails','tags')->get();
I want to use where like in user name or tag name, How can I use multiple where like on user name and tag name????
Do you want something like this?
$users= User::with('userDetails','tags')
->where('name', 'LIKE',"%{$search}%")
->orWhereHas('tags', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
$search is a variable here.
First of all, sorry for my english, I'm from france.
I try to make a simple SQL request, but I'm stucked, and I don't know why. So here are a simplification of what I want to do:
2 tables:
user_profil: iduser , idprofil
profil : idprofil, profil
What I want:
I'd like to get the list of the profils which one user don't have. So if a user have a profil in user_profil, I don't want to have this profil in the result of the request.
Infact, if the user have the profil, I don't want this profil to be in the result of the request.
I tried:
select distinct (p.idprofil), p.profil
from profil p, user_profil u
where p.idprofil != u.idprofil and u.iduser = X
But it doesn't work (it works if a user have only one profil, but if he has at least 2 profils, it doesn't work.)
Use not in or not exists:
select u.*
from user_profil u
where u.idprofil not in (select p.idprofil from profil p);
Say I have the following tables
User
__________
id
username
email
FriendGame
__________
id
useroneid
usertwoid
status
I want to get games that the current user is part of, so I do this:
SELECT *
FROM FriendGame
WHERE useroneid=1663702020516206
OR usertwoid=1663702020516206
AND STATUS =1;
This is fine. Now I want to join the username, but only for the user that ISNT the supplied user (1663702020516206) since in FriendGame the given user exists EITHER as useroneid or usertwoid.
You can pretty much translate your logic directly into an on clause:
SELECT fg.*
FROM FriendGame fg JOIN
User u
ON (fg.useroneid = 1663702020516206 and fg.usertwoid = u.id) or
(fg.usertwoid = 1663702020516206 and fg.useroneid = u.id)
WHERE 1663702020516206 in (fg.useroneid, fg.usertwoid) AND
STATUS = 1;
Actually, the where clause is not necessary to get the right result set, but I think it makes the intention of the query clearer.
I'm trying to create a query that checks an inserted value against a field. I'm trying to accomplish this using a sub-query, but got stuck. What I'm trying to accomplish:
(message1) User1 says: Hello User2
(message2) ChatX says: User1, user2 said hello to you!
I figured that in order to accomplish that, I need a subquery within the like statement.
SELECT chat.id, chat.userid, chat.message, user.userid, user.username
FROM chat, user
WHERE LOWER(message) LIKE CONCAT('hello ', (SELECT user.username FROM user WHERE XXX = user.username))
AND chat.userid = user.userid
The XXX in the LIKE statement, is the username someone said hello to. It's there it should check against the user table and if the message matches a user output ChatX's line. My question, how do I make XXX work and how do I set it?
SELECT c.id, c.message,
sender.userid, sender.username,
receiver.userid, receiver.username
FROM chat c
JOIN user sender ON c.userid = sender.userid
JOIN user receiver ON LOWER(message) like CONCAT('hello ', receiver.username)