Is this supposed to count the appearances of each link_id on the table?
SELECT link_id, count(*) FROM table group by link_id
I think it should, but if I just execute
SELECT * FROM table
I get different results. For example, for link 7 I get a count of 40 in the first query, but using 'select *' i see that there are only 4 rows of link 7... What's going on?
Yes it is supposed to do that,
Surely it would be easier to do a
SELECT DISTINCT count(link_id) FROM table
This would give you a single row containing the amount of link_id's
Alternatively
SELECT link_id,count(*) FROM table GROUP BY link_id's
Returns multiple rows containing the count of each
With regard to the original question you mention there are multiple rows per id, are you doing a join any where?
I get different results. For example, for link 7 I get a count of 40 in the first query, but using 'select *' i see that there are only 4 rows of link 7... What's going on?
Are you sure phpMyAdmin or similar isn't limiting the amount of rows you are seeing?
Related
I am trying to do a simple test where I'm pulling from a table the information of a specific part number as such:
SELECT *
FROM table_name
WHERE part_no IN ('abc123')
This returns 25 rows. Now I want to count the number that meet the "accepted" condition in a specific column but the result is limited to only the 10 most recent. My approach is to write it as follows:
Select Count(*)
FROM table_name
WHERE part_no IN ('abc123') AND lot IN ('accepted')
ORDER BY date DESC
LIMIT 10
I'm having a hard time to get the ORDER BY and LIMIT operations to work. I could use help just getting it to limit appropriately, and I can figure out the rest from there.
Edit: I understand that the operations are happening on the COUNT which only returns one row with a value; but I put the second clip to show where I am stuck in my thought process.
Your query SELECT Count(*) FROM ... will always return exactly one row.
It's not 100% clear what exactly you want to do, but if you want to know how many of the last 10 have been accepted, you could use a subquery - something like:
SELECT COUNT(*) FROM (
SELECT lot
FROM table_name
WHERE part_no IN ('abc123')
ORDER BY date DESC
LIMIT 10
)
WHERE lot IN ('accepted')
The inner query will return the 10 most recent rows for part abc123, then the outer query will count the accepted ones.
There are also other solution (for example, you could have the inner query output a field that is 0 when the part is not accepted and 1 when the part is accepted, then take the sum). Depending on which exact dialect/database you are using, you may also have more elegant options.
Select count returns ONE ROW therefore the ORDER BY and the LIMIT will not work on the results
My database is called: (training_session)
I try to print out some information from my data, but I do not want to have any duplicates. I do get it somehow, may someone tell me what I do wrong?
SELECT DISTINCT athlete_id AND duration FROM training_session
SELECT DISTINCT athlete_id, duration FROM training_session
It works perfectly if i use only one column, but when I add another. it does not work.
I think you misunderstood the use of DISTINCT.
There is big difference between using DISTINCT and GROUP BY.
Both have some sort of goal, but they have different purpose.
You use DISTINCT if you want to show a series of columns and never repeat. That means you dont care about calculations or group function aggregates. DISTINCT will show different RESULTS if you keep adding more columns in your SELECT (if the table has many columns)
You use GROUP BY if you want to show "distinctively" on a certain selected columns and you use group function to calculate the data related to it. Therefore you use GROUP BY if you want to use group functions.
Please check group functions you can use in this link.
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html
EDIT 1:
It seems like you are trying to get the "latest" of a certain athlete, I'll assume the current scenario if there is no ID.
Here is my alternate solution:
SELECT a.athlete_id ,
( SELECT b.duration
FROM training_session as b
WHERE b.athlete_id = a.athlete_id -- connect
ORDER BY [latest column to sort] DESC
LIMIT 1
) last_duration
FROM training_session as a
GROUP BY a.athlete_id
ORDER BY a.athlete_id
This syntax is called IN-SELECT subquery. With the help of LIMIT 1, it shows the topmost record. In-select subquery must have 1 record to return or else it shows error.
MySQL's DISTINCT clause is used to filter out duplicate recordsets.
If your query was SELECT DISTINCT athlete_id FROM training_session then your output would be:
athlete_id
----------
1
2
3
4
5
6
As soon as you add another column to your query (in your example, the column called duration) then each record resulting from your query are unique, hence the results you're getting. In other words the query is working correctly.
I have two tables called tc_revenue and tc_rates.
tc_revenue contains :- code, revenue, startDate, endDate
tc_rate contains :- code, tier, payout, startDate, endDate
Now I need to get records where code = 100 and records should be unique..
I have used this query
SELECT *
FROM task_code_rates
LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
WHERE task_code_rates.code = 105;
But I am getting repeated records help me to find the correct solution.
eg:
in this example every record is repeated 2 time
Thanks
Use a group by for whatever field you need unique. For example, if you want one row per code, then:
SELECT * FROM task_code_rates LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
where task_code_rates.code = 105
group by task_code_revenue.code, task_code_revenue.tier
If code admits duplicates in both tables and you perform join only using code, then you will get the cartessian product between all matching rows from one table and all matching rows from the other.
If you have 5 records with code 100 in first table and 2 records with code 100 in second table, you'll get 5 times 2 results, all combinations between matching rows from the left and the right.
Unless you have duplicates inside one (or both) tables, all 10 results will differ in colums coming either from one table, the other or both.
But if you were expecting to get two combined rows and three rows from first table with nulls for second table columns, this will not happen.
This is how joins work, and anyway, how should the database decide which rows to combine if it didn't generate all combinations and let you decide in where clause?
Maybe you need to add more criteria to the ON clause, such as also matching dates?
is there any alternatives for this MySQL query?
SELECT * FROM type WHERE tid='1' OR tid='2';
here type is the table and tid is the id of the table and i want to select first 2 rows any way
totally there are 3 rows
the above query is working but at certain times server displays nothing ,everyone suggests it is because the server is getting confused due to the query given
any alternatives please....
the IDs are not guaranteed to be gapless. They most likely aren't.
What you need is the keyword LIMIT.
Just as Garr Godfrey already answered, you sort the table by the id (ascending by default) and then limit the results to 2 at maximum
SELECT tid, foo, bar FROM type ORDER BY tid LIMIT 2
You should have a look at the basic SQL keywords. They already do most of the stuff you usually need
maybe if those IDs don't exist you'd have problem, but there is nothing confusing about that query. Try
SELECT * FROM `type` ORDER BY ID LIMIT 2
that will select the lowest 2 ids
I have a big database of users.
i have a query to filter them out and i encode in JSON the results.
i want to have at the same time the total number of the query ("Result found: 507")
but i dont want to load them all 507 but just the first 25...
i guess i have to write two queries, one for calc the toal and one to laod the first 25 right? any better solution?
thanks!
select *, count(id) as c FROM table
UNION ALL
select *, 0 as c FROM table
LIMIT 25
something like this gives 26 records, first one being a duplicate but having the total count in field 'c'
i think this gives the right answer
SQL_CALC_FOUND_ROWS and FOUND_ROWS()
via http://www.arraystudio.com/as-workshop/mysql-get-total-number-of-rows-when-using-limit.html