hello please help me out regarding this query ,I am fetching data from different table The problem i am facing is that in the table there are similar colum name like employee have and user has also name . The query work perfectly but i am wordering about how i can display this data as
$data["employee.name"]
$data["user.name"]
here is the query:
SELECT task.employee_id , task.user_id , task.service_id, user.name,
user.pic_path , employee.name ,employee.pic_path
FROM task
INNER JOIN employee ON employee.pno = task.employee_id
INNER JOIN user ON user.pno = task.user_id
INNER JOIN service ON service.service_id = task.service_id ";
SELECT user.name AS username, employee.name AS employeename
You get the point.
There are two steps:
You need to define a column alias for at least one of the two columns in the SQL statement:
SELECT t.employee_id,
t.user_id,
t.service_id,
u.name AS user_name,
u.pic_path,
e.name AS employee_name,
e.pic_path
FROM TASK t
JOIN EMPLOYEE e ON e.pno = t.employee_id
JOIN USER u ON ur.pno = t.user_id
JOIN SERVICE s ON s.service_id = t.service_id
Then you need to update the PHP logic to use the column aliases:
$empname = $data["employee_name"];
$username = $data["user_name"];
Related
I have two tables team and bobotteam, and then I was try this query:
SELECT team.id , bobotteam.teamfight/MAX(bobotteam.teamfight)
FROM team
INNER JOIN bobotteam on team.id = bobotteam.id
Why data only show 1 data even though I have two data in the bobotteam table.
IMAGE
Please try this:
SELECT team.id , bobotteam.teamfight/(select MAX(bobotteam.teamfight) from bobotteam)
FROM team
INNER JOIN bobotteam on team.id = bobotteam.id
I have 5 table,
user, plan, achievement, user_plan, user_achievement
The strucutre
User
id
name
Plan
id
name
achievement
id
name
*planID
user_plan
*planid
*userid
user_achievement
*achievementid
*userid
*createDate
And I use Codeigniter (Doesn't matter , what I would like to have is the mysql query)
$this->db->select('*');
$this->db->from('user');
$query = $this->db->get();
Actually, for each plan , there is a list of achievement, if it is not achieved, it is not stored at the user_achievement, so how can I get "for each user, get 1) his name 2)the plan he joins, and 3)the achievement he achieve and not achieve (need to highlight the achieved)"?
Thanks.
use join
$this->db->select('i.id,u.name,up.planid,p.name as planname,ua.achievementid,ach.name as ach_name');
$this->db->from('user as u');
$this->db->join('user_plan as up','up.userid = u.id','left');
$this->db->join('plan as p','p.id = up.planid','left');
$this->db->join('user_achievement as ua','ua.userid = u.id','left');
$this->db->join('achievement as ach','ach.id = ua.achievementid','left');
$query = $this->db->get();
You need to LEFT JOIN between tables user, plan,achievement and user_achievement like below
select u.name as username,
p.name as planname,
a.name as achivementname
from User u
left join user_achievement ua on u.id = ua.userid
and ua.createDate is not null
left join achivement a on ua.achievementid = a.id
left join Plan p on a.planid = p.id
Suppose I have two table
Table Name group
column 1: giver
column 2: acceptor
Table name userinfo
column 1: name
column 2: status
i want to select giver,acceptor and userinfo.status that from group table where giver or acceptor whose name is zakir that giver or acceptor exist in uerinfo table as name.
Need Help to write sql statement for taht query..
Thanks in advance... :)
What you are referring to is also known as the INNER JOIN clause in an SQL Statement.
Depending on the relationship you can create an INNER JOIN to potentially connect the two variables that are identical.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Extracted from W3Schools.com
Try the below query,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp JOIN userinfo as ui
on ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Or try this without join,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp, userinfo as ui
WHERE ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Try this:
SELECT `group`.`giver`, `group`.`acceptor`, `userinfo`.`status`
FROM `group`, `userinfo`
WHERE (`group`.`giver` = 'zakir' OR `group`.`acceptor` = 'zakir')
AND `userinfo`.`name` = 'zakir'
select g.giver, g.acceptor u.status
from group g, userinfo u
where u.name = 'zakir'
and (g.giver = u.name or g.acceptor = u.name)
It should do a part of the job.
This assumes you want the status for both the giver and the acceptor
May need a little tweaking for mysql syntax
Select giver, g.status, acceptor, a.status
FROM GROUP
join userinfo as g on group.giver = g.name
join userinfo as a on group.acceptor = a.name
where (giver = 'zakir' or acceptor = 'zakir')
I have a user table, e.g.
userId
userName
and I have a message table, e.g.
messageId
messageToId
messageFromId
messageContent
I am trying to make a query to pull a message, but also get the user names from the user table based on the messageToId and messageFromId.
I have done this before with only 1 field between tables, e.g.
SELECT message.*, user.userName
FROM message, user
WHERE user.userId = message.messageToId
AND messageId = (whatever)
But I am having trouble with 2 links.
I want the result as follows:
messageId
messageToId
toUserName
messageFromId
fromUserName
messageContent
Any help would be much appreciated, or if someone had another way of attempting a private message system with PHP/MySQL.
You just have to use joins and different table aliases:
SELECT m.*, u1.userName AS toUserName, u2.username AS fromUserName
FROM message m INNER JOIN user u1 ON m.messageToId = u1.userId
INNER JOIN user u2 ON m.messageFromId = u2.userId
WHERE messageId = "XXX";
You need to use a join from to achieve this:
SELECT `m`.*,
`to`.`userName` AS `to`,
`from`.`userName` AS `from`,
FROM `message` `m`
JOIN `user` `to` ON `m`.`messageToId` = `to`.`userId`
JOIN `user` `from` ON `m`.`messageFromId` = `from`.`userId`
WHERE `m`.`messageId` = 1
So you join against the user table twice to get both users for a particular message. To do this you need to use table aliases as I have done with to and from so that you distinguish between them.
I have also used a field alias to get their usernames separately eg:
`to`.`username` AS `from`
Will this work?
SELECT b.userName AS author, c.userName AS reciever, a.messageId, a.messageContent FROM message a JOIN user b ON a.messageFromId = b.userId JOIN user c ON a.messageToId = c.userId
I have the code here to join two tables. However I don't want to get the password element from the Accounts database. How could I do this?
"SELECT f.*, a.*
FROM Following as f
JOIN Accounts as a on f.followingUserID = a.id
WHERE `followingUserID` = '$acID'
There is no SQL convention for "all columns EXCEPT FOR ..." -- it's either all, or you define the list by hand:
SELECT f.*,
a.col1, a.col2,
a.`col name using spaces not good`
FROM FOLLOWING as f
JOIN ACCOUNTS as a on f.followingUserID = a.id
WHERE f.followingUserID = '$acID'
Name the columns instead of retrieving them all.
Instead of a.*, :
a.ColumnName1, a.ColumnName2, etc....
If you don't want to select a password element you will need to change the a.* to select each column individually i.e.
SELECT f.*, a.account_id, a.name
FROM following as f
JOIN accounts as a on f.followingUserId = a.id
WHERE followingUserID = '$acID'