Query For Counting Unique Values
I'm trying to find out if I can construct a query to MySQL that will return the number of unique values in a given column. The reason is because I have a column that just contains just the YEAR of a given report. I want to see how many different values populate the YEAR column through the entire query result so I know how many tables to render on screen (one table for each year).
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Counting By The Unique Values
Put simply I want to count how many rows there are in the database with each value for the collum such as the collum 'username' (e.g. how many rows have evenstar7139 for 'username'?). When this code, whatever it is, is run through the database I should have numbers for how many rows each different username has. (e.g. evenstar has 207, nova has 225, roonie-roe has 98, etc.) this also needs to order the results by highest number first descending to lowest number last.And if I could limit it by something like the 10 highest that would be great too.
Counting Unique
I have the following code... $query = "SELECT DISTINCT newsid FROM comments"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ which works fine. My questions is there any way to determine which newsid shows the most amount of times. I'm trying to make a "top5" sort of things and it would make it much easier.
Counting Unique Items
I have a set of ids(for different items) in a table so when I do mysql> select viewer_id from observations; +-----------+ | viewer_id | +-----------+ | 1 | | 6 | | 1 | | 2 | | 1 | | 3 | | 3 | | 2 | | 2 | | 1 | +-----------+ 10 rows in set (0.06 sec) I get all, but I want to know how many unique viewers there are (there are only 4). I know I can do mysql> select distinct viewer_id from observations order by 1; +-----------+ | viewer_id | +-----------+ | 1 | | 2 | | 3 | | 6 | +-----------+ 4 rows in set (0.00 sec) But I don't know how to put them together (using a subquery). I tried mysql> count (*) from observations where viewer_id = ( select distinct viewer_id from observations);
Counting The Same Field Wit Different Values
Is it possible to count the diferent values a field may have with a simple query?? Let's suppose this data: ---------------- FIELD ---------------- 1 2 3 4 2 2 3 2 I want a query that returns this: ---------------------------- VALUE # HOWMANY --------------------------- 1 # 1 2 # 4 3 # 2 4 # 1 Is it posible to include a condition when counting?? COUNT((WHERE...))
Counting Values That First Occur During A Date Range
I don't know why the query for this is eluding me.. I haven't had a problem coming up with a query for something in a while. I've got a table with two relevant columns, a name and a datetime created_at. The name can occur on many rows at various times. I am creating a list of all names whose first entry into the table was during a user-specified time frame. I have that working fine. It looks similar to this: SELECT name, MIN(created_at) as `min_time` FROM table GROUP BY name HAVING `min_time` BETWEEN ? AND ? In order to page this list, to display only 20 rows at a time, I need to also count how many names appeared during the time frame. For some reason I can't think of the COUNT() to do that...
Counting Occurrences Of Values In Table Column.
I have two tables; Table 1; Agent_ID UserID First_Name Last_Name 1ShadShad Mortazavi 2Harry Harry Potter Table 2; Recording_IDAgent_ID Status GSM_File etc 11 000001.gsm 21 0 00002.gsm 32 100003.gsm 41 200004.gsm 51 100005.gsm 61 200006.gsm 72 100007.gsm Status 0 = Red, 1 = Yellow and 2 = Green I would like a query that returns the number of occurrences of Red, Yellow, Green against each agent so the data returned would look something like this; NameREDYellow Green Shad Mortazavi 2 0 2 Harry Potter 0 2 0 I'm using MySQL version 3.23.58 I can do this programmatically in Perl with several query's; but this is CPU intensive. If I could get this in one query I it would save time.
Unique Values
I have a rather large database table that has a column with maybe 20 different values repeated on and off throughout the database (around 4000 records total) Could someone shoot me a query example of how I can pull each value from this column once to create the list of the 20 or so reoccuring values? Lets call the column work_type. I think I'm maybe just missing knowledge of a particular query command for the WHERE portion maybe?
Unique Values
I need to do the following: From a table that have this fields : Id, Account1,Account2,Account3,Account4 I have to get all the DISTINCT values in the accounts fields (Account1,Account2,Account3,Account4) Here is a example: Id Account1 Account2 Account3 Account4 1 111 222 Null Null 2222 333 Null Null 3 Null Null 111 444 The result should be Accounts ----------- 111 222 333 444
Help With Selecting Unique Values
I have a table called 'pictures', this holds information for every picture in an art exhibition. Its structure is (catno, title, artistfirstname, artistsurname, size, medium,price,dimensions) sample data would be something like. 1, birds, James, Quinn, 23*23, watercolour, 355, unsold 2, snake, David, Blake, 43*43, pencil, 499, sold 3, lion, David, Blake, 25*53, pencil, 250, unsold 4, tiger, Adam, Healy, 63*73, watercolour, 235, sold 5, fish, John, Smith, 12*15, charcoal, 450, sold 6, eagle, Peter, Blake, 54*54 pencil, 950, unsold I need a page that lists all the exhibiting artists and was wanting to use the database to generate this rather than having to do it manually. Artists can have multiple pictures in the system. Also some artists will share surnames. I need a statement that pulls out all the unique artists, giving a result like: James Quinn David Blake Adam Healy John Smith Peter Blake
Returning Unique Values
I currently have a table called “pages” containing the following columns: pageid, domainid, location, lastread I've put together a query that returns 10 rows with the lowest “lastread” values, for which this works fine: SELECT * FROM pages ORDER BY lastread ACS LIMIT 0,10; I now the have the added complication that I want to make sure each value in “domainid” is unique. That is to say, not repeated within the 10 results returned. I though I might be able to do something using “GROUP BY domainid” but that cannot just work on the one column as far as I can see.
Finding Unique Values
I have a table with a box field and a box2 field ... I want to grab all unique box numbers ... if a box number is in box and box2 I only want to pull it once ... I tried a bunch of different queries but none seem to do what I want.
Count Unique Values
I want a table show me how many of each year we have in the db.I tried this:Code: SELECT year,COUNT(DISTINCT year) FROM rbindex WHERE courtID = 1 But I got a MySQL error: Quote: #1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT()...) with no GROUP columns is illegal if there is no GROUP BY clause Is there another way I can do this? I want my results to look like this ----------------- year | total ---------------- 2004 | 20 2003 | 5
Making Values Of Two Columns Unique
1. Two columns, one is CHAR type, second is INTEGER. None of them is Unique, but combination of them must be. How to make this in MySQL 4.1.10? 2. What kind of Index (INDEX, UNIQUE INDEX,...) I should make on this columns to get better performance?
Assigning Unique ID To Distinct Values
I have a table `books` with booktitle and authorname columns. In my applicatoin booktitle has unique values but authorname doesn't. ie an author can have many books but not the other way around. I need to add more author info in the database. So I need a new `authors` table with authorname as a column (along with address, email, phone etc) and an authorid primary key. Then I need to replace books.authorname column with a column containing the corresponding authorids How can this be done in sql?
Returning All Unique Values From A Table Field
I wrote a select that works fine but I am looking at it and thinking that the query might require mysql to scan EVERY row before giving me my answer. Lets pretend I have a table with fruits and want to return all the available colors of those fruits that I currently have in the database. Is this an efficient or horribly inefficient statement? "select color from fruits group by color" It seems to return what I want: fruits: ------ blue green yellow ... I am just wondering if it goes through EVERY row and groups them? If I have the table indexed by color am I allright?
Primary VS Unique Keys & NULL Values
My table: ID: INT SET_ON: TIMESTAMP CLOSED: DATETIME OPTION1: ENUM OPTION2, etc... When a record is created, we populate all fields except CLOSED, which remains the default of NULL. This is the current record for this ID. We're tracking changes, so if a change is made to the options for an ID, we update the current record with NOW() to the CLOSED field and create another record. My question is about how best to key this (InnoDB) table. PRIMARY doens't allow columns with NULL values, so using ID and CLOSED won't work (unless I switch to using 0 filled value instead of NULL, which I'd rather not.) ID & SET_ON would work, but I'd like to restrict each ID to a single current record. I can add a UNIQUE key with ID & CLOSED, indexing ID again, but that seems wasteful. This also makes the table's PRIMARY key seem a bit depreciated... Is there a better way to get what I want?
Selecting Unique Values From A Table (NOT DISTINCT!)
Lets say we have a table that has the field photo_id and has values of it many times. For example 245 can exist any number of times (1, 5, 8, e.t.c.). I want to select the values that exist in the table ONLY ONCE. Please note that i dont want to select each value once (using DISTINCT), that would return all the values that exist in the table. For example lets say the table has the following values: 245 563 776 224 563 563 776 776 776 776 The query i want to make should return only the values 245 and 224 as 563 and 776 exist many times in that table.
Creating Column Of Unique Values In Order To Have A Primary Key
I have a flat file that has no columns with unique values.In other words, there is no column available that I can define as the PRIMARY KEY, and thus, cannot incorporate the data into a table. So, my question is this: How do I modify the table so that I can insert a column of unique values for each record? A column of unique values would be something as simple as a letter followed by the row number of the record (e.g, T1, T2983, etc.).
Enforcing Unique Field Values Accross Multiple Tables
I have 2 tables (t1,t2) with unique keys defined in each. In addition to enforcing unique key values in each table, I would also like to enforce unique key values accross both tables (ex. If key value 'XXX' appears in t1 I don't want to allow entry of key value 'XXX' in t2 and vice versa).
Selecting Multiple Columnn Values Into 1 Distinct/unique List
I have a table with several fields for email (primary email, alternative email, contact email) Some times, some of these fields will be blank and sometimes the same address will be enter for both primary and contact. I use this table and these fields to do a mailout but I don't want to mail people twice because there address occurs in more than 1 column. I need to end up with a list containing all email values from those 3 columns that is unique (no duplicates). I can do this by checking each one and putting it into a php array but thought there may be a nicer way to do it in the SQL?
Counting Query
Is there a MySQL function that i can use to just count the number of rows in a table instead of listing all the IDs and running a command on the result?
Counting Query
I need a slect statement to return the count of rof rows if a particular field matches a criteria similar to the COUNTIF function in Excel. We are using mySQL 3.23.58. Is there a way to do this and if so how?
Counting Records In 2 Tables Using 1 Query
I have these 2 queries. SELECT count(*) gifts FROM gift g WHERE g.this and g.that SELECT count(*) events FROM events e WHERE e.this and e.the other thing is there a way to put these into one query..... SELECT count(g.*) gifts, count(e.*) FROM gift g, event e WHERE . . . . so far nothing seems to be working .....
Unique Query Help
I have two tables Table 1 has 3 unique URL (for e.g. hotmail.com) and Table 2 has 4 records of (hotmail.com)...The only way to display the combination for a particular URL is to use group by. Table 1 id url 1 hotmail.com 2 sitepoint.com 3 youtube.com Table 2 id userid (user who added) url notes 1 user1 hotmail.com good site 2. user2 hotmail.com email site 3. user3 hotmail.com great site 4. user4 hotmail.com cool site Now I want to JOIN table 1 and table 2 and display unique records for hotmail.com...Only way i can do is by using group by...I don't want to use that. And it doesn't really matter if it display record by any user, but as long as it is only one record for hotmail.com in Table 2 Can anyone please help me If I use inner join from Table 1 to table 2 ON URL, it still shows 4 records of that url, though i only want to show the first one if repeat record occur.
Unique Query
I don't know if this can be done but here goes I have a mysql table with the fields id, user_name, L1 and L2 the first query is Code: select L1 from tbl_name where id=some_value now I want it to check if L1 is NULL if it is not then check if L2 is null if it is not take the value of L1 as some value and redo the query and if both L1 and L2 are not NULL in this query go back to the original L2 and take that value as some_value and do the query and continue like this until a NULL value is found then do an update I am not very good at explaining so if this is not clear please ask
Unique/distinctrow Query ???
I have two tables each with two fields I want to retrieve: name and number. I only want to retrieve the rows from which a year=2007. This is my query: SELECT table1.name, table1.num, table2.name, table2.num FROM table1, table2 WHERE table1.year=2007 AND table2.year=2007 When I do this directly in MySQL it brings back the results: ANDREA 1 ALVIN 1 ANDREA 1 BARBARA 2 ANDREA 1 THREE-E 3 BARRY 2 ALVIN 1 BARRY 2 BARBARA 2 BARRY 2 THREE-E 3 where table1 is on the left and 2 is on the right. This data is only in the actual table once (in other words, for whatever reason it's returning each row twice. The problem is I'm trying to execute it in PHP and it only returns table 2's data. Is it something in my query that I am missing or do I need to focus on the PHP side? I've been studying the query altering it a little with DISTINCTROW and whatever else I can think of but can't quite get the desired results.
Query To Show Unique Entries
In the table word_list is a list of words. This field is not unique so you can get 3 of the same word (they have different attributes in other fields). How can I alter this query below so that I only get unique entries in the word field (so that the query does not return more than one of the same word).
Buliding A Query:selecting Unique Records
I am nearly positive that there is some way to have mysql select only the first occurance of a data entity on a select. Maybe a better explanation is given by example. Think of having a table of employees, with one of the columns being their bosses email address. Now I want to do a select statement on the bosses email addresses that only returns one entry for each bosses email, even though that bosses email address will occour > 1 in the table. (more than 1 employee has the same boss...) I could parse out the duplicates when I get to PHP but I would rather have mysql do the work for me.
How To Write Query To Select The Max(version) For Each Unique File_name Record?
I am a MySQL newbie trying to write a query that selects file_name records possessing the highest numbered version for that unique file_name. I show sample data and two trial queries below. Logically I want to use max(version) as a constraint in a Where Clause. However, the max() function is not allowed directly in a where clause. I have contemplated a second table to track the max version for each file name. I would like to structure the data in an efficient manner for query performance when the data set grows to many thousands of unique file_name records with many hundreds of versions each........
Help With 3 Table Query & Counting Rows In Third Table
I have three tables, a members, a vendors and a products. I'm using a query to grab all of the rows from the first two tables, matching on a primary key. This is easy and works fine. However, I'm trying to pull the number of products from the third table and it's giving me trouble. Basically, I can count the products for each vendor if products exist, but if there are no products, instead of returning 0, no rows are returning. current query looks like this: SELECT t1.*, t2.*, count(*) AS count FROM vendors as t1, members AS t2, products AS t3 WHERE t2.mem_id = t1.mem_id AND t3.vendors_id = t1.vendors_id GROUP BY t1.vendors_id; So, all of the vendors that have zero products are being left out of the results. Does anyone know if it's possible to achieve this without using a temp table?
Distinct Values In Mysql Query
I have mysql query but i am not sure about it,bcoz it return many rows.. I want like distinct values from three tables with ic number. Which is posted by user... "SELECT distinct loguser.icnumber,access.acccode,dealerdtl.name,loguser.fullname,access.period,loguser.staffcode, DATE_FORMAT(access.actdate, '%d-%m-%Y ')As formatteddate1, DATE_FORMAT(loguser.creationdate, '%d-%m-%Y %H:%i:%s')As formatteddate FROM dealerdtl,loguser,access where access.icnumber=loguser.icnumber AND loguser.icnumber = '$custic'"; This query return many rows i dont know why, i want only one row which is inserted by the user as customer id.
Reference To Already Retrieved Values In Query
As a simple example, say there is table 'namelist' with column 'names' as char(20). I would like to do something akin to: select namelist.names as mynames, left(mynames,2) as initials; In this example, I could just do left(namelist.names,2), but in more complex cases a value retrieved may have had a more complex logic behind it, e.g., if a bunch of nested if() statements. It would seem logical that if a value is already retrieved then I should be able to refer to it within the same query, assuming that the original value does not need to be recalculated. I know I could use a view to do this, but it adds another layer of complexity, and I am not sure that the values would not be recalculated each time the value is needed. If someone could point out to me the correct terminology for this kind of thing I believe I should then be able to look up the information myself. My efforts to find this information on google and the mysql website were unsuccessful.
NULL Values In SELECT Query
I need to know how to push null values to the bottom of a query sorted by ASC. I know if you sort by DESC the null values are placed last. How can I push the null values to the end of a query while still sorting using ASC? Anyone?
Accumulating Values Inside A Query
I only have experience of simple queries, and this one is a bit beyond me. I've done some research, and I think it might be possible to do within a single query, but I'm not sure. I have a single table. It contains columns (among others): date, number. I want to produce a table of DATE, and new_number_count, where 'new_number_count' is the number of numbers on day with date DATE that do not have rows on previous DATEs. For example, if this is my table:
Update Query Where Values Will Come From Other Table
I'm creating an update query which the value will come from another table. I have here my current query which unfortunately makes the system hangs. Probably because of the query itself is not properly coded. update boxes b inner join messages m on b.ctnnumber = m.ctnno set b.consigneerecv = m.CName, b.consigneerecvdate = m.DateRcv, b.phrecventered = "Y", b.PhilStatus = "delivered", b.prevreleasestatus = b.releasestatus, b.releasestatus = "delivered", b.PhilStatusDate = m.smsrecvdate, b.phdelprice = "0.00", b.phdelamt = "0.00", b.recvrelation = m.Relation, b.APRecventered = m.smsRecvDate where b.consigneerecv = '' or b.consigneerecv = 'NA' or b.consigneerecv is null; I'm thinking revising it so that it will not cause the system to hang but I don't know how. Guys please help me with this one. I also have this another idea which probably will not work. My idea was something like this: Update table1 set table1.column1 = (select table2.column1 where table2.column1 = table1.column1), table1.column2 = (select table2.column2 where table2.column1 = table1.column1), .....
How To Generate Sequence Values During Query
I have query like below: SELECT part_no,part_nm,qty FROM tb_stok_out ORDER BY part_no as result: part_no part_nm qty aaa asdfd 3 abab sdfsdf 4 abab adfdf 5 Is it possible in mysql to generate sequence number using query, so the result will be like below : 1 aaa asdfd 3 2 abab sdfsdf 4 3 abab adfdf 5 ... etc
Difficult Query - Need To Group Results By Id And Sum Values
I have got the meat of this query done but I am facing a problem. I am doing a VAT analysis whereby I have every shoe that is over size 7 I pay tax on and every shoe below that I don't pay tax on. I also pay tax on accessories. What I want the query to return is this: Date | ShopperID | VATable Amount | Non VATable Amount| 2006-3-1 | 802135 | 146.95 | 54.00 | Basically each shopperID will only appear once which is why I am grouping the results (which I have managed). But I also need it to total up all the VATable amounts that it finds too for both the fields on the right above. I can get the date and order number without difficulty and I have managed to get the data like this so far where I have the same ShopperID where a shopper has bought more than 1 product: +------------+----------+-----------------+---------------------+ | Date | ShopperID| VATable Amount | Non VATable Amount | +------------+----------+------------+---------------+ | 2006-09-04 | 805284 | 0 | 64.00 | | 2006-09-04 | 805287 | 2.95 | 0 | <-- Here a | 2006-09-04 | 805287 | 3.25 | 0 | customer has | 2006-09-04 | 805287 | 3.45 | 0 | bought 4 products | 2006-09-04 | 805287 | 4.95 | 0 | - I need total | 2006-09-04 | 805327 | 0 | 53.95 | under each | 2006-09-04 | 805335 | 0 | 58.95 | ShopperID | 2006-09-04 | 805414 | 0 | 64.95 | | 2006-09-04 | 805414 | 3.25 | 0 | | 2006-09-04 | 805414 | 0 | 64.00 | | 2006-09-04 | 805414 | 0 | 69.00 | | 2006-09-04 | 805423 | 0 | 64.95 | | 2006-09-04 | 805423 | 0 | 69.00 | | 2006-09-04 | 805423 | 0 | 64.00 | | 2006-09-04 | 805423 | 3.25 | 0 | +------------+-----------+----------------+--------------------+ Heres the query currently getting these results (without the GROUP BY ShopperID). SELECT Date,orders.ShopperID, CASE WHEN SUBSTRING_INDEX(Product,',',-1) REGEXP '( )?(UK)?( )?[^1-9][7-9]( )?(' THEN items_ordered.price -- If size 7 or above add price WHEN SUBSTRING_INDEX(Product,',',-1) REGEXP '( )?(UK)?( )?[^1-9]10( )?(' THEN items_ordered.price -- If size 7 or above add price WHEN SUBSTRING_INDEX(Product,',',-1) REGEXP '( )?(UK)?( )?[^1-9]11( )?(' THEN items_ordered.price -- If size 7 or above add price WHEN SUBSTRING_INDEX(Product,',',1) REGEXP 'Insole|Helmet|Laces|Wheels|Removal|Protection|Bag' THEN items_ordered.price -- If it is an accessory add VAT ELSE 0 END AS 'VATable Amount', CASE WHEN SUBSTRING_INDEX(Product,',',-1) REGEXP '( )?( )?(Kids)?( )?( )?(UK)?( )?( )?(Kids)?( )?( )?[^1-9][1|2|3|4|5|6]( )?(' THEN items_ordered.price -- If size 1 - 6 add price to other column WHEN SUBSTRING_INDEX(Product,',',-1) REGEXP '( )?( )?(UK)?( )?( )?Kids( )?( )?(UK)?( )?12|13( )?( )?(' THEN items_ordered.price -- If size kids 12/13 add price to other column ELSE 0 END AS 'Non VATable Amount' FROM items_ordered,orders WHERE (items_ordered.ShopperID = orders.ShopperID) AND (Date >= ��-1-30') AND (Date <= ��-9-31');
A Query To Select A Column When A Percentage Of Values Non Zero?
I wounder whether some of the experts out there might be able to help me with a problem I'm having. I do not know whether this is possible or not... I have a large table of stock price data which is straight-forward enought. I can select prices based on a ticker and date ranges. However, what I'd like to do is to select prices only when, say 75% of them are non-zero (with the goal of eliminating new/suspended/delisted stocks). Of course I could just select where price > 0, but then I might get only a few rows where this is the case. What I would like to do is always get the full date range of prices, but only if >75% are there.
Showing Query Result With Default Values When No Data Available
I have a scenario that user can ask for certain data say income & expense for last 3 months on monthly basis. I have wrote following query which works well but it has an issue: Let say I have asked for data of FEB 2007, JAN 2007, DEC 2006 using the following query it shows me correct result when all three months have some data but it doesn't show me the desired result when you don't have data in one or more months. Any suggestion to get result when there is no data:
Truncated Time Values Using TIMEDIFF With ORDER BY Query
I'm using the following query: SELECT glider, timestamp, TIMEDIFF(timestamp, UTC_TIMESTAMP()) AS last_contact FROM surfacings INNER JOIN (SELECT MAX(timestamp) AS most_recent FROM surfacings GROUP BY glider) AS tmp WHERE surfacings.timestamp = tmp.most_recent; to calculate the amount of time that has elapsed since the last inserted timestamp for each glider. Everything works fine: ....
Unique Combinations Of Model And Make (was "Query Question")
I'm trying to write a query and was wondering if someone could help me formulate it. Here is an example table I'm working with: Table: vehicles Model Make Ford Taurus Ford Taurus Ford F150 Chevy Tahoe Chevy Blazer Chevy Tahoe I want a query that returns all unique combinations of model and make. So the result set should be: Model Make Ford Taurus Ford F150 Chevy Blazer Chevy Tahoe I know how to return distinct values for one variable, but not for two like that. Any help is greatly appreciated.
Count Of *unique* Visits By Page For Each Day (was "Need Help With Query")
Using MySQL 4.1 I have a table 'orders' with the following fields: ipaddress pagenum xtime (which is just the yearmonthday) this is the query: SELECT distinct pagenum,ipaddress,xtime,count(pagenum) AS pagenumtotal FROM `orders` group by pagenum,xtime ORDER by xtime, pagenum asc What I am trying to do is return a count of *unique* visits by page for each day. This query returns a count of *all* visits by page for each day, including repeats. So if a visitors hits a certain page on the site 10 times and adds a record 10 times, the count result will include all 10 records in the count. I want this to only count as 1.
Next And Prev DisplayOrder Values (was "Help With MySQL Query")
I have a table 'Album' containing columns 'PageID' and 'DisplayOrder' (both INT). On each page that is displayed I want to have 'previous' and 'next' links to other pages, which are arranged via the DisplayOrder column. At the moment I'm doing this in 2 separate queries: SELECT DisplayOrder FROM Album WHERE PageID = $currentPage $currentNo = [result of last query] SELECT PageID FROM Album WHERE DisplayOrder IN ($currentNo-1, $currentNo+1) Highly simplified but you get the idea. Would it be possible to combine this into one query?
Sub Counting For Each Row
For each row in my table, I'm getting stuck doing a count. An extract of the table is as follows: user_id | name | favourite_user 1 | "person 1" | 2 2 | "person 2" | 1 3 | "person 3" | 1 4 | "person 4" | 2 5 | "person 5" | 2 I'm trying to select all rows from the table, and at the same time, a count of how popular each person is. In two queries, this can be achieved by: SELECT * FROM TABLE; SELECT favourite_user, COUNT(*) FROM TABLE GROUP BY favourite_user How can I combine the queries so that for each person I return their data and a count of their popularity?
|