I have three tables
Clients:
Client ID
First name
Last name
Transactions:
Trans ID
ClientID
RepresentativeID
Order ID
Representatives:
Representative ID
First name
Last name
I need to display all of the transactions information, with the representative name and client name that occurred in a specific date. Is this query right?
SELECT *
FROM [Transactions], Clients.first name, Clients.last name, Representatives.first name, Representatives. last name
INNER JOIN [Clients]
ON Transactions.ClientID= Clients.Client ID
INNER JOIN [Representatives]
ON Transactions.RepresntativeID = Representatives.Represntative ID
WHERE Transactions.OrderDate BETWEEN '1996-09-18' AND '1996-11-27';
Is that right or did i get all wrong?
Joining conditions are fine. However, depending on type of OrderDate column you may need to use MySQL's STR_TO_DATE function, e.g.:
WHERE STR_TO_DATE(`OrderDate`,'%d-%m-%Y')
BETWEEN '1996-09-18' AND '1996-11-27'
Update
We also need to update SELECT and FROM syntax:
SELECT t.Trans_ID, c.first name, c.last_name, r.first name, r.last_name
FROM Transactions t INNER JOIN Clients c on t.ClientID= c.Client_ID
INNER JOIN Representatives r
ON t.RepresntativeID = r.Represntative_ID
Related
I have two tables. One for Customer and other is for managing login history. Now what I want to do is to get the customer name and email from customer table, and last login time from loghistory table.
I have used the query:
select cust_email, login_time, cust_email
from customer
left join login on customer.cust_id = login.cust_id
GROUP by cust_id
It gives me the correct result showing NULL for the customers who never logged in, but gives the first login time of other customers not the latest one. How do I get the last login time of customer?
Use MAX() & columns which are in SELECT statement must be aggregated or it should be with GROUP BY clause :
select c.cust_email, max(l.login_time)
from customer c left join
login l
on c.cust_id = l.cust_id
GROUP BY c.cust_email;
Note : Use table alias that is easy to understand & read.
use max() function
select cust_email,max(login_time) as max_login_time from customer
left join login on customer.cust_id=login.cust_id
GROUP by cust_email
I think you should use simple "Join" not left join with a order by clause. Try this:
select cust_email, login_time, cust_email
from customer
join login on customer.cust_id = login.cust_id
GROUP by cust_id
Order by login_time esc
I just started learning SQL last night. I'm having trouble displaying data using multiple JOIN statements. The tables I have are:
Table: CUSTOMER
Contains CustomerID, Country, Last Name
Table: TRANS
Contains CustomerID, TransactionID, DateSold, WorkID
Table: WORK
Contains WorkID, Title, Description
Here's my query:
Select CUSTOMER.LastName, CUSTOMER.CustomerID, WORK.WorkID,
Description, Title
FROM CUSTOMER JOIN TRANS
ON CUSTOMER.CustomerID = TRANS.CustomerID
JOIN WORK
ON TRANS.WorkID = WORK.WorkID
WHERE DateSold = '11/17/2014'
GROUP BY CUSTOMER.CustomerID, TRANS.CustomerID, CUSTOMER.LastName,
WORK.WorkID, Title, DateSold, Description
Note that in the select statement, I've deliberately left out a few items that appear in the GROUP BY statement, just for the sake of this post. (Their inclusion in the SELECT statement doesn't cause program to execute properly.)
All that appears is the GROUP BY statement, but no actual data. Please help me with what I'm doing wrong. Thank you.
Use the STR_TO_DATE function in mysql to convert the date string.
WHERE DateSole = STR_TO_DATE('11/17/2014', '%m/%d/%Y')
You can join one table with two different table but you have to start from the common table.
For how you wrote the query you are saying to join CUSTOMER first with TRANS and then with WORK but the conditions are wrong for this situation (and it is not what you want to do).
Select CUSTOMER.LastName, CUSTOMER.CustomerID, WORK.WorkID,
Description, Title
FROM TRANS JOIN CUSTOMER
ON CUSTOMER.CustomerID = TRANS.CustomerID
JOIN WORK
ON TRANS.WorkID = WORK.WorkID
WHERE DateSold = '11/17/2014'
GROUP BY CUSTOMER.CustomerID, TRANS.CustomerID, CUSTOMER.LastName,
WORK.WorkID, Title, DateSold, Description
It is TRANS that you join first with CUSTOMER and then with WORK.
I'm trying to join two MySQL Tables to get a staffid from one and a username that is tied to the staffid in another table so that I just have the username displayed
SELECT ost_ticket.staff_id, count(*) as numbers
FROM ost_ticket AS us
JOIN ost_staff AS re
ON re.username = us.userid
GROUP BY ost_ticket.staff_id;
From what I've been reading about joins this should work?
EDIT: I've ran it and I get unknown column 'ost_ticket.staff_id' in field list
You've aliased the two tables (as re and us), so you'll need to use the aliases. Also, it seems you've joined to the ost_staff table to retrieve the username, so if you want to display the user name, you need to select + group by it, and not the staff_id column.
SELECT re.username, count(*) as numbers
FROM ost_ticket AS us
JOIN ost_staff AS re
ON re.userid = us.userid
GROUP BY re.username;
(substitute your actual PK for ost_staff - I've assumed userid)
SELECT us.staff_id, re.username, count(*) as numbers
FROM ost_ticket AS us
JOIN ost_staff AS re
ON re.userid= us.staff_id
GROUP BY us.staff_id;
The real issue
Involved tables and their columns
accounts [id,name]
rooms [id,name,topic,owner]
room_admins [account_id,room_id]
Q: Get all rooms with their admin- and owner ids.
Where "all" of course has a condition to it (above: WHERE name LIKE ...)
Admins and owners should be returned in one column just called "admins". I tried to concatenate them above into one string.
What I tried
I came up with a solution, but it requires the use of an omnious external variable ":room_id" that changes on each outer SELECT and makes therefore no sense at all.
SELECT id,name,topic,
(SELECT GROUP_CONCAT(admins.account_id) AS owner
FROM
(SELECT account_id
FROM `room_admins`
WHERE room_id=:room_id
UNION
SELECT owner FROM `rooms` WHERE id=:room_id) admins) AS owner
FROM `rooms`
WHERE name LIKE "%htm%" OR topic LIKE "%htm%" LIMIT 20
Well, I haven't given this a deep thought... but I've just came up with this (sample data would have been useful to make tests... so this is just a blind answer).
select id, name, topic, group_concat(owner_admin) from (
select id, name, topic, owner owner_admin from rooms
union
select id, name, topic, account_id from rooms
left join room_admins on id = room_id
) s
where name like "%htm%" or topic like "%htm%"
group by id, name, topic
Basically I'm just generating a derived table with owner and admins mixed in one column. Then performing the grouping on that mixed column.
Most of the times, when you want to select and display dependent data, you want to use a JOIN. In this case, you want to join the rooms with their admins, so basically:
SELECT r.id, r.name, r.topic, a.id
FROM rooms r
LEFT JOIN admins a
ON r.id = a.room_id
WHERE :condition
Since you have one additional admin not in the admins table (the room owner), you have to (self) join a second time:
SELECT r.id, r.name, r.topic, a.id
FROM rooms r
LEFT JOIN admins a
ON r.id = a.room_id
LEFT JOIN rooms o
ON r.id = o.id
WHERE :condition
This doesn't give us any new information, but your question states that you want to return the list of admins in a single field. So, finally, putting it all together:
SELECT r.id, r.name, r.topic, GROUP_CONCAT(a.id)
FROM rooms r
LEFT JOIN
(
SELECT id, room_id FROM admins
UNION SELECT room.owner AS id, rooms.id AS room_id FROM rooms
) a
ON r.id = a.room_id
WHERE :condition
GROUP BY r.id
But to avoid this ugly sub-select-union clause, I'd advise you to put the room owner into your admin table too.
Suppose the following situation.
Persons assigned to tasks, and I want to return Person id, Person Name, the number of tasks completed by each person from the following tables.
Table Name - Field Name
Person - id, Name
Task_Person_Combi - Task_id, Person_id
Task* - returns id of Task (actually this is LEFT Joined table which returns id of persons)
(Task has over 100,000 rows, and the query has to be quick well less than 1 second)
After reading MySQL statement combining a join and a count?, I'm trying the following. (but this doesn't seem to work, and I'm kind of lost)
SELECT id, Name,
(
SELECT COUNT(*)
FROM Task_Person_Combi C
WHERE P.id=C.Person_id AND C.Task IN (SELECT id FROM Task* - this is Joined table)
) AS Count
FROM Person P
WHERE id>0
HAVING Count>0
ORDER BY Name
Please help.
Try this?
SELECT id, Name,
COUNT(T.ID) AS TaskCount
FROM Person AS P
INNER JOIN Task_Person_Combi AS C ON P.id=C.Person_id
LEFT JOIN TASK AS T ON C.Task = T.id
WHERE id>0
AND T.id IS NOT NULL
GROUP BY id,Name
HAVING COUNT(T.ID)>0
ORDER BY Name