We have build a very simple referral system that tracks userID's without cookies and referrals for social media. I am trying to create something like a 'leader board' so I can show the UserID of the top leaders in the database.
I'm trying to combine these 2 queries into 1 query.
SELECT
Count(users.AffiliateID) AS affiliate,
users.AffiliateID
FROM
users
group by affiliateID
order by affiliate desc
This generates an output where the variable 'affiliate' contains the USERID of the top referring affiliate. In this case the #1 person is affiliateID = 5297dc41331235
What I then do is look up the name of the person with this query.
Select name from users where UserIDHash = "5297dc41331235";
How can I rewrite the query above so that it looks up the value of the name field as a column that references the value of the AffiliateID i.e. where AffiliateID=UserIDHash on each record?
Your help is greatly appreciated.
Thanks!
Related
Sir,
The concept of my project is ,the logged user can upload three different photos and the deatils are stored in b_photodetails table and the fields are(id,photoid,student_id,userid,count,likes,subdate ) idas1,2,3...ect photoid means the name of the store photo file userid and count is the the number of photoes uploaded ie 1,2 and 3anly.and likes is the total number of likes for each photos.And i Have another table like_master containing the like details and the fields are(like_id,userid, liked_date) ,the like id in this table is same as id in the b_photodetails table ,and the liked_date is the date in which the photo is liked.So i need to get the number of likes between two particular
dates.Please help me to frame a query to solve this in MySQL.
For eg:If we click a like for where id=1,the likes in b_photodetails will be 1 and the like_master table will be(like_id,userid, liked_date) (1,userid eg:sree,2014-01-28)
I don't really know for sure what you are asking, but, here is my stab in the dark:
select sum(likes) from like_master where liked_date between 'date1' and 'date2';
or possibly
select sum(lm.likes)
from b_photodetails as b
right join like_master as lm
on b.userid = lm.userid
where lm.liked_date between 'date1' and 'date2';
I don't understand much what you are asking but may be it is like this
Select b.*,(SELECT COUNT(like_id) from like WHERE b.id = like.photo_id) as total_likes FROM b.photodetails
I am writing a query to extract results from a table with a condition to exclude some results from the table.
Ex: I have a table with students name column (100 students). I want to run a query to extract details of 95 students and exclude 5 students. Could you help me out with this?.
Here is more information:
Thanks for your answer. I am sorry if my question was unclear.
Here is more information what I am trying to run.
I have a task queue ID
Task Queue 1 = Documents manually processed.
Task Queue 2 = Documents exported to another application
Task Queue 2 includes documents manually processed and documents automatically processed to another application. I am trying to run total number of documents exported to another application (Task Queue 2) and exclude Task Queue 1 from these results.
I tried the code as given below , but I did not get any results.
Select WorkQueueHistory.id from WorkQueueHistory where
TaskId='2' NOT IN( Select WorkQueueHistory.id from WorkQueueHistory where TaskId='20') and
creationStampUtc>='2013-01-01 00:00:00.000'
and creationStampUtc<='2013-12-31 00:00:00.000'
WorkqueueHistory is the table
TaskID is the task number
I want to run results from taskID 2 but want to exclude task ID 20.
could you help me out with this?.
What criteria do you have for excluding the 5 students? That is kind of the key here
For example, if you wanted to exclude all of the men you might do
SELECT Name, Gender FROM Students WHERE Gender = 'Male'
Or to exclude those called Mark maybe
SELECT Name FROM Students WHERE Name LIKE '%Mark %'
But it depends on the criteria
UPDATE: *with specifics to update, something along these lines (without seeing your table)
SELECT q2.DocName
FROM Queue2 q2
WHERE q2.Id NOT IN (SELECT Id FROM Queue1)
Assuming you have an Id column or something which is common across both queues - you need some common identifier be it name or Id
I have some booking data from a pair of views in MySQL. They match columns perfectly, and the main difference is a booking code that is placed in one of these rows.
The context is as follows: this is for calculating numbers for a sports camp. People are booked in, but can do extra activities.
View 1: All specialist bookings (say: a football class).
View 2: A general group.
Due to the old software, the booking process results in many people booking for the general group and then are upgraded to the old class. This is further complicated by some things elsewhere in the business.
To be clear - View 1 actually contains some (but are not exclusively all) people from within View 2. There's an intersection of the two groups. Obviously people can't be in two groups at once (there's only one of them!).
Finding all people who are in View 2 is of course easy... as is View 1. BUT, I need to produce a report which is basically:
"View 1" overwriting "View 2"... or put another way:
"View 1" [sort of] UNION "View 2"
However: I'm not sure the best way of doing this as there are added complications:
Each row is as approximately (with other stuff omitted) as follows:
User ID Timeslot Activity
1 A Football
1 A General
2 A General
3 A Football
As you can see, these rows all concern timeslot A:
- User 2 does general activities.
- User 3 does football.
- User 1 does football AND general.
AS these items are non unique, the above is a UNION (distinct), as there are no truly distinct rows.
The output I need is as follows:
User ID Timeslot Activity
1 A Football
2 A General
3 A Football
Here, Football has taken "precedence" over "general", and thus I get the picture of where people are at any time.
This UNION has a distinct clause on a number of fields, but ignores others.
So: does anyone know how to do what amounts to:
"add two tables together and overwrite one of them if it's the same timeslot"
Or something like a:
"selective distinct on UNION DISTINCT".
Cheers
Rick
Try this:
SELECT *
FROM
(SELECT *,
IF(Activity='General',1,0) AS order_column
FROM `Table1`
ORDER BY order_column) AS tmp
GROUP BY UserId
This will add an order_column to your original table that as value 1 if the Activity value is general; Doing this we can select this temporary table ordering by this column (ascending order) and all record with general activity comes after all others. After that we can simply select the result of this temporary table grouping by user id. The group by clouse without any aggregate function takes the first record that match.
EDIT:
If you don't to use group by without aggregate function this is an 'ugly' alternative:
SELECT UserId,
Timeslot,
SUBSTRING(MAX(CASE Activity WHEN "General" THEN "00General" WHEN "Football" THEN "01Football" ELSE Activity END) , 3)
FROM `Table1`
GROUP BY UserId,
Timeslot LIMIT 0 ,
30
Here we need to define each possible value for Activity.
I have 2 tables , First table is named Ticket
Ticket
ID
Subject
Owner
second table is
TicketLinkedNames
ID
TicketID
ContactID
LinkedReason
from the above structure you understand that I can link the table ticket with some other names so when I entered to the ticket form I can see in a grid below all the link names that are associated with this ticket. Am ok here?
My main problem is that I have one main form that I want to display all the tickets that the owner is some contact and also all the tickets that this contact is appear as link name in other tickets..
sort them out by unique records and display them.
I am really confused of what kind of select query should I use, I have tried several like:
Select * from Ticket,TicketLinkedNames where Owner=ContactID
but returns wrong records. I have use inner join between ID=TicketID but also return wrong records.
I am really confused, please if someone could help me i will really appreciated.
I am using Microsoft Access 2007.
make 2 selects
SUB select1:
select distinct from table Ticket
for the owner that not contained in TicketLinkedNames
SUB Select2:
select distinct from table TicketLinkedNames
for the owner
then make a union between the 2
then you got the solution
I'am using a simple newsletter-script where different categories for one user are possible. But I want to get the different categories in one row like 1,2,3
The tables:
newsletter_emails
id email category
1 test#test.com 1
2 test#test.com 2
newsletter_categories
id name
1 firstcategory
2 secondcategory
But what Iam looking for is like this:
newsletter_emails
user_id email category
1 test#test.com 1,2
2 person#person.com 1
what's the best solution for this?
PS: The User can select his own Categorys at the profile page. (maybe with Mysql Update?)
SQL and the relational data model aren't exactly made for this kind of thing. You can do either of the following:
use a simple SELECT query on the first table, then in your consuming code, iterate over the result, fetching the corresponding rows from the second table and combining them into a string (how you'd do this exactly depends on the language you're using)
use a JOIN on both tables, iterate over the result set and accumulate values from table 2 as long as the ID from table 1 remains the same. This is harder to code than the first solution, and the result set you're pulling from the DB is larger, but you'll get away with just one query.
use DBMS-specific extensions to the SQL standard (e.g. GROUP_CONCAT) to achieve this. You'll get exactly what you asked for, but your SQL queries won't be as portable.
This is a many-to-many relationship case. Instead of having comma separated category ids make an associative table between newsletter_emails and newsletter_categories like user_category having the following schema:
user_id category
1 1
1 2
2 1
This way you won't have to do string processing if a user unsubscribes from a category. You will just have to remove the row from the user_category table.
Try this (completely untested):
SELECT id AS user_id, email, GROUP_CONCAT(category) AS category FROM newsletter_emails GROUP BY email ORDER BY user_id ASC;