I'm have 2 tables: table for customers and table for activity. Im trying to find how many customers have 2 activity specific. For example, how many customers was bought and arrived to the branch. (i have line in the activity table for every act that the customer did.) this is the query:
SELECT COUNT(*) as total FROM activity
WHERE activity = 'arrived' AND
customerid IN
(SELECT DISTINCT(customerid) FROM activity
WHERE activity = 'bought')
but i need to know how much bought AFTER they arrvied, because there are customers that bought from the website, than arrived to the branch after 1 year for example. so i want only the customer that arrived and than bought.
i tried this:
SELECT COUNT(*) as total, daten as odaten FROM activity
WHERE activity = 'arrived' AND
customerid IN
(SELECT DISTINCT(customerid), daten as tdaten FROM activity
WHERE activity = 'bought') HAVING odaten < tdaten
but its not working.. any idea?
You are in a wrong direction. You should not use sub query. Instead, you should try self join. Here is my solution:
--activity customerid daten
select count(customerid) as finalcount from(
select distinct t1.customerid
from tablename t1
inner join tablename t2 on t1.customerid=t2.customerid
where t1.activity='arrivived' and t2.activity='bought' and t1.daten<t2.daten
)t
You could try something like this
SELECT COUNT(a.*)
FROM activity a
INNER JOIN
(SELECT customerid, MAX(daten) as BoughtDate
FROM activity
WHERE activity = 'bought'
GROUP BY customerid) t
ON a.customerid = t.customerid
WHERE a.activity = 'arrived'
AND a.daten < t.BoughtDate
Related
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;
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 have two tables,
TABLE 1 has many of each client and campaign and is very large
TABLE 2 has only one of each client and campaign and is small.
So I want to get the lastest(highest ID) from TABLE 1 where it matches the client and campaign in TABLE 2 and only one of each.
I have tried MAX, and playing with the order by etc, but cant get it working....
The results I get are choosing the lowest ID from TABLE 1 (I want highest)
$result2 = mysql_query("SELECT table1.client,table1.campaign,table1.id
FROM table1
LEFT OUTER JOIN
table2
ON (table2.client = table1.client)
AND (table2.campaign = table1.campaign )
WHERE (table2.enabled != 'disabled')
group by campaign asc
order by client,campaign,id asc
");
Help needed....
SELECT * FROM table1
INNER JOIN
(
SELECT MAX(table1.id) AS id FROM table1
INNER JOIN table2 ON table2.client = table1.client AND table2.campaign=table1.campaign and table2.enabled != 'disabled'
GROUP BY table1.client, table1.campaign
) AS m ON m.id = table1.id
I think that's what you're asking for. For each combination of client and campaign that exists in each table, it will give you the highest ID in table 1.
i have two table. Records and Villas
Records table:
ID, VID (Villa ID), NAME, PRICE
Villas table:
ID, NAME (Villa NAME), PHOTOS etc.
I using this SQL:
SELECT records.id, villa_name AS (SELECT name FROM villas WHERE id = records.vid), records.name
FROM records WHERE records.id = 5
What is wrong ?
Try
SELECT records.id, records.name AS record_name, villas.name AS villa_name FROM records INNER JOIN villas ON records.vid = villas.id WHERE records.id = 5
Edit: Incorporated suggestion from Mark Bannister, below.
You should use JOIN here.
SELECT r.id, r.name, v.name
FROM records r
INNER JOIN villas v ON v.id = r.vid
WHERE records.id = 5;
I have 3 tables structured like so:
activity table:
activity_id, user_id, type, date
reviews table:
review_id, activity_id, fishery_id, review, date
updates table:
update_id, activity_id, update, date
I want to call the all the reviews and updates which are linked to a user by the activity table however this query returns incorrect results, any ideas?
query('
SELECT *
FROM activity as activity
LEFT JOIN reviews AS reviews
ON activity.activity_id = reviews.activity_id
LEFT JOIN updates AS updates
ON activity.activity_id = updates.activity_id
WHERE user_id = 1
');
Thanks, Matt
You're trying to select two different sets of data, the only way I can thing of doing this in one SQL statement, is to use a union, and create some extra columns to tell the difference.
SELECT 'review' as type,r.review_id as id,r.fishery_id as fid,
r.review as review,null as update,r.date as date
FROM reviews r,activity a
WHERE r.activity_id=a.activity_id AND a.user_id=#user_id
UNION
SELECT 'updates' as type,u.update_id as id,null as fid,
null as review,u.update update,u.date as date
FROM updates u,activity a
WHERE u.activity_id=a.activity_id AND a.user_id=#user_id
You probably don't want reviews and updates in the same rows, which appears to be what you are trying to do. So, you need two queries, not one, which would be:
SELECT r.review_id, r.fishery_id, r.review, r.date
FROM reviews r
WHERE EXISTS (SELECT 1 FROM activity a
WHERE a.user_id = #user_id
AND a.activity_id = r.activity_id)
SELECT u.update_id, u.update, u.date
FROM update u
WHERE EXISTS (SELECT 1 FROM activity a
WHERE a.user_id = #user_id
AND a.activity_id = u.activity_id)
Just for reference the working query:
SELECT type, review_id, activity.activity_id, review, activity.date, fishery_id
FROM activity as activity
JOIN reviews AS reviews
ON activity.activity_id = reviews.activity_id
UNION
SELECT type, activity_id, activity.activity_id, activity, activity.date, NULL
FROM activity as activity
JOIN activity AS activity
ON activity.activity_id = activity.activity_id
WHERE activity.user_id = 1
ORDER BY date DESC