i have a query that i want to use the results from to search for other details in another table and display the results together
my initial query is to show all accounts and how many bookings have been made for the day specified
SELECT ACCOUNT, COUNT(ACCOUNT) Bookings
FROM dbo.bookings
WHERE DATEADDED = 101
GROUP BY ACCOUNT
I then want to use the "ACCOUNT" which is a code to search for the name of the account in dbo.accounts.....
ACCOUNT is equal to CODE in dbo.accounts table and i need to get the NAME from this table
Sounds like you just want to join the two tables together, so:
SELECT a.NAME, b.ACCOUNT, COUNT(*) Bookings
FROM dbo.bookings b
INNER JOIN dbo.accounts a ON a.CODE = b.ACCOUNT
WHERE b.DATEADDED = 101
GROUP BY a.NAME, b.ACCOUNT
You must be looking for something like this:
SELECT b.ACCOUNT
,a.NAME
,COUNT(ACCOUNT) Bookings
FROM dbo.bookings b
JOIN dbo.accounts a ON b.account = a.code
WHERE DATEADDED = 101
GROUP BY b.ACCOUNT
,a.NAME
Alternatively, you could use a CTE if you are using SQL Server:
WITH b(SELECT ACCOUNT, COUNT(ACCOUNT) Bookings
FROM dbo.bookings
WHERE DATEADDED = 101
GROUP BY ACCOUNT)
SELECT a.NAME
,b.*
FROM b
JOIN dbo.accounts a ON b.account = a.code;
Related
I have a table structure like following:
Users => Histories <= Brands
Histories table keeps following columns: userId, brandId, points
I have a query like following:
select b.id, sum(h.points),h.brandId
from brands b left join
histories h
on b.id = h.brandid and h.userId = 2866
group by b.id;
This query returns me brands where USER made points and didnt make any points at all..
I wanna add a filter to a brandId as well so that the query doesn't returns all results, but a single result at a time depending what brandId has been sent to the query... How can I and where can I add a statement which would return me a single record for this query?? So basically I just need to pass brandId = to something statement, leaving the query it self as it is right now ... :)
For example if brand id is 100:
select b.id, sum(h.points),h.brandId
from brands b left join
histories h
on b.id = h.brandid and h.userId = 2866
where b.id=100
group by b.id
limit 1;
I have multiple tables that are linked together on the search page. They are currently queried like this:
SELECT * FROM tripreportentries
JOIN tripreport ON tripreportentries.reportid = tripreport.tripreportid
JOIN user_details on tripreportentries.userid = user_details.userid
WHERE TripEntryHeading LIKE '%%' AND tripreportentries.ispublic = 1
ORDER BY tripreportentryid
I have since created a new table called tripphotos with the table columns named:
tripphotoid
reportid
entryid
photoname
The issue is each entry can have more than one corresponding photo in the photo table.
I am looking to find a way to JOIN the new table to the statement above but not return several rows for each entry id.
Any ideas on how to join this table?
EDIT - I also want to just tie one image to the returned row and not all of them just so I can show the image as a thumbnail on the browse page
I normally would list every column and group by the non aggregates, but mySQL will allow this I believe; given the mySQL group by extensions; and we expect the tripReport and user_Details columns to be the same for each joined photoname.
SELECT tripReport.*, user_details.*, max(photoname) as maxPhotoPerReport
FROM tripreportentries
JOIN tripreport ON tripreportentries.reportid = tripreport.tripreportid
JOIN user_details ON tripreportentries.userid = user_details.userid
JOIN tripphotos ON tripreportentries.reportid = tripPhotos.ReportID
WHERE TripEntryHeading LIKE '%%' AND tripreportentries.ispublic = 1
GROUP BY ReportId
ORDER BY tripreportentryid
or we could do this using an inline view to get the max photo for each report and then join that set to your base query.
SELECT *
FROM tripreportentries
JOIN tripreport ON tripreportentries.reportid = tripreport.tripreportid
JOIN user_details on tripreportentries.userid = user_details.userid
JOIN (SELECT max(photoName) as MaxPhotoPerReport, ReportID
FROM TripPhotos
GROUP BY ReportID) B
ON B.ReportID = tripReport.ReportId
WHERE TripEntryHeading LIKE '%%' AND tripreportentries.ispublic = 1
ORDER BY tripreportentryid
I am having a little issue here.
So I have two tables and I need to fetch data in what i think is a complex way.
So below is a summary of the two tables
clients
client_id
name
booked [default is 0]
accommodation
accommodation_id
client_id
date
price
What I would like to have is select all client id's from tbl
clients where booked is 1
then using the client_ids select all rows in accommodation whose
client_id is an of those returned in step 1
What i had in mind proved difficult for me
$select_accomodation = "SELECT * FROM `accommodation` WHERE `booked` = 1";
if($select_accomodation_run = #mysql_query($select_accomodation))
{
//awesome code that does no 2
}
What is the best possible way to accomplish tasks 1 and 2. Hopefully in one mysql statement
If you just want to select all accommodations for booked clients you could do
SELECT a.*
FROM accommodation a
INNER JOIN clients c ON a.client_id = c.client_ID
WHERE c.booked = 1
Try this:
select t1.client_id, t2.accommodation_id, t2.client_id, t2.data, t2.price from clients t1 JOIN accommodation t2 on t1.client_id = t2.client_id WHERE t1.booked = 1
My thought, is first write a subquery that gets the Ids you want for part 1, which is:
SELECT client_id FROM clients WHERE booked = 1
Then, you can use that subquery inside another query for the accomodations table using the IN clause
SELECT a.* FROM accomodation a WHERE a.client_id IN (SELECT c.client_id FROM clients c WHERE c.booked = 1);
I`m fetching data from sql with left join query, I want to filter the query to be more specific,
In my database i have customers, every customer have a Group, every user have his customers. i guess you understand now what i need.
every user will see only the rows that relate to him. this is my query:
SELECT t.*,c.fname,e.DISCODE,e.AREA,e.COLOR
FROM $tbl_name AS t
LEFT JOIN customers AS c ON t.MCcode = c.MCcode
LEFT JOIN eventcodes AS e ON t.MCcode = e.MCcode AND t.CODE=e.CODE
ORDER By `id` DESC LIMIT $start, $limit
ON customers table i have field named Group, and when user is connected i have his Group name, so how i can filter this query that only where ( for example ) c.Group = '$Group', all the rows are found in $tbl_name, and the other details i`m fetching by joins.
thanks!
Like this
LEFT JOIN customers AS c ON t.MCcode = c.MCcode and c.Group = '$Group'
Im trying to construct a query that goes over 3 tables and im COMPLETELY stumped ... my knowledge limit is basic 1 table query and i need some help before i stick my head in a blender.
I have the following query
SELECT * FROM internalrole WHERE introle = $imarole
Im fine with that part .. its the next thats getting me all stressed.
That query returns the following columns ( id, user_id, introle, proven, used )
What i then need to do is take the user_id from the results returned and use it to get the following
SELECT * FROM users WHERE id = user_id(from previous query) AND archive = 0 and status = 8
I need to put that into 1 query, but wait, theres more .... from the results there, i need to check if that user's 'id' is in the availability table, if it is, check the date ( column name is date ) and if it matches todays date, dont return that one user.
I need to put all that in one query :S ... i have NO IDEA how to do it, thinking about it makes my head shake ... If someone could help me out, i would be eternaly grateful.
Cheers,
Use INNER JOIN, which links tables to each other based on a common attribute (typically a primary - foreign key relationship)
say an attribute, 'id', links table1 and table2
SELECT t1.att1, t2.att2
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id --essentially, this links ids that are equal with each other together to make one large table row
To add more tables, just add more join clauses.
SELECT u.*
FROM internalrole ir
INNER JOIN users u
ON ir.user_id = u.id
AND u.archive = 0
AND u.status = 8
LEFT JOIN availability a
ON ir.user_id = a.user_id
AND a.date = CURDATE()
WHERE ir.introle = $imarole
AND a.user_id IS NULL /* User does NOT exist in availability table w/ today's date */
EDIT: This second query is based on the comments below, asking to show only users who do exist in the availability table.
SELECT u.*
FROM internalrole ir
INNER JOIN users u
ON ir.user_id = u.id
AND u.archive = 0
AND u.status = 8
INNER JOIN availability a
ON ir.user_id = a.user_id
WHERE ir.introle = $imarole
Hmm, maybe something like this
SELECT * FROM users WHERE id IN (SELECT user_id FROM internalrole WHERE introle = $imarole) AND archive = 0 and status = 8;
A handy thing for me to remember is that tables are essentially arrays in SQL.
HTH!
Nested queries are your friend.
SELECT * FROM users WHERE id in (SELECT user_id FROM internalrole WHERE introle = $imarole) AND archive = 0 and status = 8
Alternatively joins:
SELECT * FROM users INNER JOIN internalrole ON users.id = internalrole.user_id WHERE internalrole.user_id = $imarole AND users.archive = 0 and users.status = 8