I have 3 different tables names as state, config, and user
state: columns
--------------
taskid
class
state
queue time
running time
config: columns
---------------
class
confname
user: columns
-------------
username
userid
I can join the state and config tables using class column, but in my result set, I want to add the username as well, but there is not matching columns to join. But I can do this via inclusing one more table tasks that has a relationship with the state table.
tasks: columns
--------------
taskid,
userid,
taskname
Using this table, I want to generate the output columns like below.
username - from users table
confname - from config table
state, queue time, running time - from the state table
I tried the following query, but its giving zero results. Can someone help me with the better logic?
select
p.username,
c.confname,
s.state,
s.queue_time/1000000 ||' sec' as queue_time,
s.exec_time/1000000 ||' sec' as execution_time
from
state as s join
config c on s.class=c.class
join ((select distinct(userid)as user from taks q join state s on q.taskid=s.taskid) j
join
user p on j.user=p.username)
Your query has a syntax error. There are more joins than ons.
You are describing something like this:
from state s join
config c
on s.class = c.class left join
task t
on j.taskid = s.taskid left join
user u
on t.user = u.username
Of course, if there are no matching tasks or users, then those columns will be empty.
Related
I have a database with 2 tables in it one is 'enlistments' and the other one is 'users'. In the enlistments table I have a user_id and in the users table I have a name. I want to get the name of the user which belongs to the id.
I know I need to do this with an inner join like this:
SELECT enlistments.round_id, users.name
FROM enlistments
INNER JOIN users
ON enlistments.user_id=users.name
WHERE enlistments.activity_id = 1;
However I get this error: Warning: #1292 Truncated incorrect DOUBLE value
I did some research and found out it has to do with comparing an int with a string but I don't know how to solve the problem.
This is how my database looks like
join on is the condition you use to join the tables. Here it's enlistments.user_id=users.id.
select e.round_id
,u.name
from enlistments e join users u on u.id = e.user_id
where activity_id = 1
round_id
name
1
test2
Fiddle
To validate and be sure you are pulling back the exact data desired, I usually provide aliases for each column brought back and make sure to bring back the join columns also. It's good practice to label where the columns returned originated.
SELECT
Enlistments.UserID as Enlistments_UserID,
Users.ID as Users_ID,
enlistments.round_id as Enlistments_RoundID,
users.name as Users_Name
FROM enlistments
INNER JOIN users
ON enlistments.user_id=users.id
WHERE enlistments.activity_id = 1;
SELECT EN.round_id, US.name
FROM enlistments EN
INNER JOIN users US
ON US.name= CAST(EN.user_id AS VARCHAR)
WHERE EN.activity_id = 1
What you are needing is the function cast that can convert any kind of data into another, so you'll pass your integer value as the first argument followed by "AS '%DATATYPE'" where %DATATYPE is the kind of data you want to achieve.
In your case:
SELECT CAST(123456 AS VARCHAR)
-- RETURNS : '123456'
Anyway, I’m not sure that you can be able to join these two tables with the join you are using.
For more help please share some data.
This is task table:
This is user table:
I want to select user tasks.
I would give from backend ("given_to_user) id.
But The thing is I want that SELECTED data would have usernames instead of Id which is (created_by_user and given_to_user).
SELECTED table would look like this.
Example:
How to achieve what I want?
Or maybe I designed poorly my tables that It is difficult to select data I need? :)
task table has to id values that are foreign keys to user table.
I tried many thinks but couldn't get desired result.
You did not design poorly the tables.
In fact this is common practice to store the ids that reference columns in other tables. You just need to learn to implement joins:
SELECT
task.id, task.title, task.information, user.usename AS created_by, user2.usename AS given_to
FROM
(task INNER JOIN user ON task.created_by_user = user.id)
INNER JOIN user AS user2 ON task.created_by_user = user2.id;
Do you just want two joins?
select t.*, uc.username as created_by_username,
ug.username as given_to_username
from task t left join
users uc
on t.created_by_user = uc.id left join
users ug
on t.given_to_user = ug.id;
This uses left join in case one of the user ids is missing.
I have an sql table that has 2 columns, one is id_User from a table named applicationUsers and the other one id_Schedule from the table schedule, when i insert information on them i have to do it by the id, but also i have tha table showed on a datagridview, it shows the 2 colums correct, but how can i get for example i have in the main table 1 1, that the name of the user 1 is David and the name of the schedule is special.
How can i show the name from the user 1(David) and the name from the schedule 1(special) in the datagridview?
You would need to execute a query that joins all three tables, e.g.
SELECT u.UserName, s.ScheduleName
FROM User u INNER JOIN UserSchedule us
ON u.UserID = us.UserID INNER JOIN Schedule s
ON s.ScheduleID = us.ScheduleID
Populate a DataTable from that query and bind it to a DataGridView and you'll see the user name and schedule name in the two columns.
I have two database tables. I'll simplify for the sake of this question
USERS
id : int
email: varchar
EVENTS:
user_id: int
event_name: varchar
I'd like to build a query that'll give me an output like:
ID Email Event name
------- --------- ----------------------
1 r#r.com Test event 1
2 d#d.com Test event 2
Obviously the query will select * from EVENTS but will also need to match the user_id in EVENTS via join I assume, to be able to select the e-mail in the USERS table corresponding to the id.
Any ideas how the syntax would look like?
Try this
SELECT
u.id AS ID,
u.email AS Email,
e.event_name AS Event
FROM
users u JOIN events e ON u.id = e.event_id
SQL is best understood with potatoes: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
I have 3 database tables:
Workoutput which defines the work done by employees.
employeeinfo which defines relationships for each person. The three important fields are child, parent and relation. A person can be listed
a couple of times with different relationships i.e,
child parent relation
12 3 2
12 43 4
With the relation referring to whether the parent is mentor or manager
And finally a standard employee table which has comprehensive listings about the employee id, name, login etc.
I would like to have a results table which defines all the people above that one worker on a daily basis, but have it defined in one row
on a daily basis. At the moment I can only define them on separate days with the following query.
SELECT workoutput.Day, workoutput.Employee, employee.name
FROM workoutput INNER JOIN
employeeInfo ON workoutput.ID = [employeeinfo].son INNER JOIN
[employee] ON [employeeInfo].parent = [employee].id INNER JOIN
[employee] AS [employee1] ON workoutput.[Employee ID] = [employee1].id
This returns the relationships, but there will be two rows for each day, as the majority of people will have two people above them (Mentor and Manager).
29/03/2010 00:00:00 Employee1 Manager 3
29/03/2010 00:00:00 Employee1 Mentor 1
and I would like
29/03/2010 00:00:00 Employee1 Mentor 1 Manager3
I also have another table at my disposal, which define the relationships it has two rows, id and type of relation, with id referring to a relation id defined in the employeeinfo table.
Is that possible?
Thanks
Is there a maximum number of parents for a child (i.e. relations)? And is there one per relation?
If so, you can do this with a PIVOT to get from your current output to your desired output.
However, if there are a fixed number of roles, you could also include that in your joins from the outset:
SELECT wo.Day, wo.Employee, e.name, er1.name, er2.name, etc.
FROM workoutput AS wo
INNER JOIN [employee] AS e
ON workoutput.[Employee ID] = e.id
LEFT JOIN employeeInfo AS ei1
ON e.ID = ei1.child
AND ei1.relation = 1
LEFT JOIN [employee] AS er1
ON ei1.parent = er1.id
LEFT JOIN employeeInfo AS ei2
ON e.ID = ei2.child
AND ei2.relation = 2
LEFT JOIN [employee] AS er2
ON ei2.parent = er2.id
etc.
Not with standard SQL. The usual approach here would be to sort the table and then use it as input into a reporting tool or a custom program.
In SQL Server, it would be possible to write a stored procedure which did this. It depends on how badly you want it.
you can group on employee identifier and group_concat the parents.
http://explainextended.com/2010/06/21/group_concat-in-sql-server/
if your relation field is constant (i.e. only a set number of relationship types) you can write a CASE statement to transpose it like this...
SELECT workoutput.Day, workoutput.Employee,
CASE relation WHEN 2 THEN employee.name END AS relation2, CASE relation WHEN 4 THEN employee.name END AS relation4
FROM workoutput
INNER JOIN employeeInfo ON workoutput.ID = [employeeinfo].son
INNER JOIN [employee] ON [employeeInfo].parent = [employee].id
INNER JOIN [employee] AS [employee1] ON workoutput.[Employee ID] = [employee1].id
GROUP BY workoutput.Day, workoutput.Employee, employee.name
However, a rather glaring downside to this is that if you add new relations then you will start to get duplicate rows so this should only be used if you have rigorous version controlling in your SP's or similar
EDIT
Apologies, I missed off the GROUP BY clause :)