I have 2 tables and need to make a View table
These are 2 tables that I have:
user table with some fields: id, username, email, pass
system_log table with 2 fields: uid, message (uid means user_id)
Now, I'd like to have a view table which gives me a table with 2 fields, username and message.
Here is my problem: I need username from user table in my view table while I have uid in my system_log table. Basically, instead of having uid, I need the username
The project and list of fields in the actual project are more than these but I just made it simple here to make my points clear.
You can use inner join to get the data from both tables
CREATE VIEW `view_name` AS
SELECT
u.user_name,
s.message
FROM users u
INNER JOIN system_log s ON u.id = s.user_id;
CREATE VIEW my_view AS
SELECT u.username, s.message FROM
user u INNER JOIN system_log s
ON u.id = s.user_id
Related
Okay, so I have two tables, a news table and a users table. They are set up as below:
news:
id title user_id contents
users:
id username password
When I run this query:
SELECT news.title, users.username, news.contents FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
I can only access the user id using $query['id']. So it appears that the join is using the column names from table2, although I want them to map it to the column names of table1. So even though the column is called id in the users table, I want to access it under user_id, since I specified that in the JOIN query.
I need this because if I ever need the id from table1, they would both be mapped to the column called id and I would only be able to get the id of the users table.
So is there any way to do this? Access the column from table2 under the name of the column in table1?
In MySQL what you specify in the select statement is what it is called in the result set, so you need to tell it what to give you. you do that with the AS command
SELECT users.id AS user_id ... FROM ...
That way you can call it whatever you want
or grab the user_id from news with
SELECT news.user_id ... FROM ...
SELECT users.id AS id1, news.id AS id2, news.title, users.username, news.contents
FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
echo $query['id1'] . $query['id2'];
I have a roles table which contains various user roles.
A pivot tables joins this to my users table.
users
id | name
roles
id | title
user_role
user_id | role_id
I would like to display all roles that exist and pre tick the ones that belong to a specific user.
How would I go about this, I take it I cannot do this in mySQL?
I was considering getting all roles with one query, then with another query get all roles that belong to a specific user.
Then loop through all roles and if there is a match with the specific users roles, output a checked box instead of an unchecked one.
Is there a better way?
Something like
SELECT R.id as role_id,
R.title as role_title,
UR.user_id as user_id,
U.name as user_name
FROM Roles R
LEFT JOIN user_role UR
ON UR.role_id = R.id
AND UR.user_id = :myuserid
LEFT JOIN users U
ON U.id = UR.user_id
should return a complete list of roles, with either a NULL or the user id in the user column to indicate any roles that the user has been granted (user identified by id as :myuserid)
I think normal SQL Query can do most of the work: Checkout this SQLFIddle for sample of what your query might look like
SELECT r.* FROM roles r LEFT JOIN user_role ur ON r.id=ur.role_id WHERE ur.user_id=3;
Been bothered with this for awhile now and i think it might be how i have the joins set up.
I have two tables. Ones is called info which contains all of a users contact information. My second table called numbers has all the phonenumbers for different users. They are related by the primary id of info to the info_id of phonenumbers. I want them to join based on this relationship and I want all the phonenumbers under phonenumbers to join into the single phonenumbers column in info. The current join i am using is this.
SELECT phonenumbers p, info i FROM i.phonenumbers
INNER JOIN p.workphone
ON i.PID=p.info_id
INNER JOIN p.homephone
ON i.PID=p.info_id
INNER JOIN p.mobilephone
ON i.PID=p.info_id
all i get is the SELECT comman is deneied to user on database workphone that isnt evena database.
table info:
PID,
firstname,
lastname,
address,
email,
phonenumbers,
table phonenumbers:
PID,
workphone,
homephone,
mobilephone,
info_id,
The syntax for a join would be nice. All the tutorials just give examples and not an explanation of what the different pieces are.
JOIN syntax is
TYPE_OF_JOIN database.table ON field = field
Since you have
JOIN p.workphone ON i.PID = p.info_id
You're actually telling the DB to look for a database named p, which contains a table workphone.
Doesn't matter that you've created an alias p up in your SELECT fields list. That's a field alias, and they NOT the same as a table alias.
I'm having some trouble figuring out how I should build my database for this project i'm currently working on. Fishing-related.
I'm just not sure how to set up my tables.
Table 1(ID, username, email etc)
Table 2(fish, weight, length etc)
How do i join these two tables? Should I have a column named ID in the 2nd table aswell? Because I need to know which user uploaded what fish. I'm just not sure how to do that.
Any help is appreciated.
Yes you have to, and that is called Relation Databases this is example
Users (UserID, UserName, Password)
Fish (FishID, UserID, FishName, Length, Weight)
and then you connect them using UserID
select u.UserName, f.FishName, f.Length, f.Weight
from Users u
LEFT JOIN Fish f on (f.UserID=u.UserID)
and if you are looking for specific user then just add at the end
WHERE u.UserID=#UserID
Looking at you're table structure I think it's best to change the id name in table 1 to *user_id* and add a column in the second table also named *user_id*. Joining using the columns is then very simple using the following query:
SELECT *
FROM table1
JOIN table2 USING (user_id)
Other possibility would be to add a column named *user_id* (or something else) to table2 and create a query like:
SELECT *
FROM table1
JOIN table2 ON table2.user_id = table1.id
In this case, you set the columns to use for the join in the 'ON .. = ..' structure.
user_info table:
ui_id - PK
name
created_by - FK
users table:
user_id- PK
username
I want to display the content of user_info table. The created_by field are user_ids from users table, I want to change it into username when displaying it.
Is it possible in a single query? Please tell me how.
This would do
select ui.*, u.username from user_info ui
join users u on u.id = ui.created_by
Simply join the tables.