I have two tables gcm_users and vechicle_master with me.
gcm_users has session id named ID which is generated automatically*(A_I Primary Key)* when user registers.I use this session id in my query to identify the user at time of log in.
requirement - i want to show all the vehicle details from vehicle_master table which are related with the user from the gcm_user table.
NOTE:
when a user logs in, he should only see his own vehicle details.
Try using JOIN you can do like, put your user_id in place of 1
SELECT a.*
FROM vehicle_master a
JOIN gcm_users b
ON a.registration_no = b.registration_no
WHERE user_id = 1
Related
i am creating a restaurant review website. in my review table i have a foreign key called user_id and idk how to use it to display the username which is in the user table
my user table
my review table
so my question is how do i display the username from this? what mysql statement do i have to write. I am lost on what to do
Assuming you want to try and get the review text along with the user name from the corresponding user you can use a join to combine the info for example:
SELECT u.username, r.review_text
FROM reviews r
LEFT JOIN users u
ON (u.user_id = r.user_id)
I assumed the users table is called users and reviews table is called reviews but update those as necessary each is "aliased" as u and r respectively and then tables are joined
If the relationship between the two tables is mapped out correctly you should be able to run a query to fetch the name of each user. Try to avoid any N+1 query though
How does Twitter (or Facebook) do to retrieve a user feed?
Here is the database schema I am working on. I renamed everything to look like twitter hoping that the most of you would understand my question...
I only have two resources: users and tweets. So here are those two tables:
users:
- id
- username
tweets:
- id
- author_id
- content
Let's continue with a simple pivot table to associate tweets and users:
user_tweet:
- user_id
- tweet_id
- created_at
This pivot table is here mainly to store the retweets. But it also stores the original tweets for more convenience.
For example, let's take user 1 tweets 'something'. user 2 and user 3 retweet. The user_tweet table will have three rows.
And, let's see a last table that complicates everything: The Following System.
Every user can follow an other user. I named the table "followee_follower":
followee_follower:
- followee_id
- follower_id
The followee_id is the user.id of the person being followed
The follower_id is the user.id of the person that follows an other one
Now let's get to the SQL problem:
I'm user 1. I follow user 2, and user 3.
How can I retrieve the tweets and retweets from user 2 and user3 knowing that I want to retrieve them ordering them by the created_at field of the user_tweet table, and that I don't want to get two similar tweets.
Many thanks, any help is highly nice from you,
Have a good day/night.
EDIT: Here are some samples data from tables:
users table
tweets table
user_tweet table
followee_follower table
expected results
I'm not sure I completely understand your question (sample data would help), but I think you just need to use multiple joins:
select t.id, t.content, ut.created_at
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
order by ut.created_at
Perhaps if a user retweets, you'd want to do something like this instead to get the first tweet (assuming the id and created_at fields both should return the minimum):
select t.content, min(t.id), min(ut.created_at)
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
group by t.content
order by min(ut.created_at)
I have a rather special use case in front of me. There is to be an excel file with around a thousand entries (rows), each row represents something that the USER should pass judgment on.
Now, the entries are the same for everyone. The data that should be collected is
a) how many users like any given entry
b) what entries does any given user like
Since part of the app is already running and we have user accounts,
I thought of creating a table for each user (!) containing said excel information, adding a row for collecting the votes. I would create those tables by iteratin through the user list and creating tables like "userid_excelentries".
I don't think that's elegant. I would prefer to store the excel information only once in a table and only save the users' votes in the table "user".
The app is meant to display a table created form the excel table (I have the grid already done) and a row next to it with checkboxes. How do I structure this ? Temporary tables ? How do I store the information what each user has selected in the "user" table, since I don't know how many selections will be made a-priori ?
I had this crazy idea of actually handling the xls object through javascript, serializing it into a hash and storing that hash into a field in each user's row...but I have no clue if this is sane :o
We're facing a user count of exactly 272 - this is why I considered doing the "one table for each user" approach.
You can use 3 tables in your DB
users table
-----------
id
name
...
entries table
-------------
id
name
...
user_entries table
------------------
user_id
entry_id
user_response
To get all entries a certain user (i.e. Tom) likes you can do
select e.name
from entries e
join user_entries ue on ue.entry_id = e.id
join users u on ue.user_id = u.id
where u.name = 'tom'
and ue.user_response = 'like'
And to get the count of likes for each entry you can do
select e.name, count(ue.user_id) as likes
from entries e
join user_entries ue on ue.entry_id = e.id
where ue.user_response = 'like'
group by e.id, e.name
I've got a system on my website which is very similar to Facebook, where you can post statuses and your people can comment on your status, like it etc. This all gets inserted in the database in the following format, with child tables of the likes and comments with foreign keys set up in case the parent status gets deleted, the likes and comments get deleted with it.
I also have a friends table which contains the user ID of the user that started the friend request, the user ID of the user that has to either accept it or deny it, and the status of the record, whether it's accepted, denied or pending.
There's also a "users" table which contains the normal malarkey, such as emails, passwords etc. All records have a unique ID however, in the column "userID".
The query I have at the moment loads all statuses regardless of whether the status owner is your friend or not. The current query looks like this (I'm working in ColdFusion so ## are the variables passed to the function)
SELECT *,
(SELECT COUNT(*) FROM status_likes WHERE likeStatusID=statusID) AS StatusLikeCount,
(SELECT COUNT(*) FROM status_comments WHERE SID=statusID) AS StatusCommentCount
FROM status, users
WHERE statusOwner=userID
AND statusType='user'
ORDER BY statusDateTime DESC
LIMIT #args.indexStart#,#args.indexEnd#;
I need this query to only load statuses if the owner of the status is your friend. I can call a query to load a users friends and append a string containing the user ID's of all the friends, such as: "652,235,485,975" etc.
I tried doing an IN in the query so there was an extra line:
AND (statusOwner=#val(args.userID)# OR statusOwner IN (#usersFriendsString#))
However this brought back duplicate results and when I tried GROUP BY on the status owner, it didn't bring back records that it should have.
Any MySQL gurus out there able to help?
You should use something like that :
SELECT
s.*,
(SELECT COUNT(*) FROM status_likes WHERE likeStatusID=s.statusID) AS StatusLikeCount,
(SELECT COUNT(*) FROM status_comments WHERE SID=s.statusID) AS StatusCommentCount
FROM Users u
JOIN Friend f ON f.friendOwner = u.id
JOIN Status s ON s.statusOwner = f.id
WHERE u.id = <...>
ORDER BY s.statusDateTime DESC
You can use WHERE clause if you can't use a JOIN instruction.
Or you can use a IN instruction populated by a SELECT that retrieve all requiered status ids.
I have a MySQL db set up like so,
user_table table
user_ID PK
username
password
email
level
location
game_items table
game_item_is PK
item_name
user_inventory table
user_inventory_id PK
user_id FK
game_item_id FK
quantity
This is a db for a game i am building and i am trying to code an sql query to get the following data out of the db:
Level
Location
A list of game items the user has associated to their user_id
The data i already have is the user_id and username.
So how would i go about creating this sql statement? would i use joins? if so where?
I want to get every game_item the user has associated with their account so would this include * anywhere?
Thanks
Ok so after a while playing with the sql on phpmyadmin i got the result i wanted, if anyone would like to know here is the code:
SELECT ut.username, ut.level, ut.location, ui.quantity, ui.game_item_id
FROM user_table ut, user_inventory ui
WHERE ut.user_id = ui.user_id
AND ut.user_id =1
If I understand your question correctly, just select the level and location in one query and the inventory items in another
SELECT level, location FROM user_table WHERE id = ..
SELECT * FROM user_inventory WHERE id = ..