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.
Related
I have two different table in phpmyadmin. One is tbl_user and another is donate.
Now I want to take the column donation_date from tbl_user and all columns from the donate table. I want to join one column (donation_date) from tbl_user table and all cloumns from donate table, but dont know how to write the query.
In the below code I just wrote the query of donate table so how can I join the donation_date from the tbl_user.
Here is my details of two tables in phpmyadmin.
tbl_user table :
donate table:
$db = new PDO('mysql:host=localhost;dbname=mypro_bms','root','');
$statement = $db->prepare(
"insert into donate(passport_ic,blood_group,blood_bag,)
values(:passport_ic, :blood_group, :blood_bag)"
);
I want to take the column donation_date from tbl_user and all columns from the donate table.
Are you looking for... a simple JOIN between tables tbl_user and donate?
From your schema images, it looks like column passport_IPC can be used to join the tables. Then you can choose which columns to return in the SELECT clause:
SELECT u.donation_date, d.*
FROM tbl_user u
INNER JOIN donate d ON d.passport_IPC = u.passport_IPC
Looks like Passport_IC is the common column between the two tables?
You really should use be using numeric, indexed columns. But since you have a small DB, this should work:
SELECT d.*, u.donation_date
FROM donate d
INNER JOIN tbl_user u ON u.Passport_IC = d.Passport_IC
EDIT: You should give your a tbl_user table a primary key as well. Look into database normalization: https://en.wikipedia.org/wiki/Database_normalization
As you said, you want to take the column donation_date from tbl_user so you will select just the column nedded tbl_user.donation_date ans use the following alias donation_date and to get all columns from the donate table you can use this trick donate.*, it gets all column
SELECT
donate.* , tbl_user.donation_date as donation_date
FROM
tbl_user
JOIN
donate ON donate.passport_IPC = tbl_user.passport_IPC
Easy question for you but hard solution for me.=)
I have a database - USERS. In USERS I have two tables - USERS_INFO and EVENTS.
USERS_INFO contain next fields:
user_id
user_name
...
and
EVENTS contains next:
event_id
user_id
obj_id (this element means, for examle, when user_1 will change information
event about user_2 in this table appears record like:
event_id=1
user_id=user_1
obj_id=user_2)
So, as you can see, information about user_id, obj_1 from table EVENTS I get from table USERS_INFO in field user_id.
I connected it.
The question is = how to create right query?
I need to see something like this:
user_1 user_4 some_event_like_edit (means that user_1 changed smth in user_4)
I can create query, but it works wrong. I did -
SELECT USERS_INFO.user_name, EVENTS.event FROM USERS_INFO,EVENTS WHERE USERS_INFO.user_id=EVENT.user_id
BUt I cant create query for another field obj_id!!
Result give me the fields where EVENTS.user_id=EVENTS.obj_id
What I should do??
You just need to join to user table twice, like this:
SELECT u_1.user_name as Who_Changed,u_2.user_name as Who_Got_Changed, e.event
FROM EVENTS e
INNER JOIN USERS_INFO u_1
ON (u_1.user_id=e.user_id)
INNER JOIN USERS_INFO u_2
ON (u_2.user_id=e.obj_id)
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
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'];
So Lets say I have a user table and then a user_profile table. To connect the relationship there would be a user_id row within the user_profile table. Now when I'm building my applications I like to make my url show the username and not the user_id example:
http://www.example.com/username/profile
not:
http://www.example.com/user_id/profile
So what I find myself doing is getting the user_id through the username and then fetching the profile info which just adds an extra query for no reason. My questions is could I just make the relationship through the username which is just as unique as the id row in the users table. Or is this bad practice and I should stick with using user_id?
A much better idea would be to use the user_id to join the tables, but provide the username as a parameter. Something like:
select p.*
from users u
, profiles p
where u.id = p.user_id
and u.username = ?