I would like to search and count unique values in a column/fieldname of a MySQL Result Set.
In other words, when some lines of one table are stored in a mysql resultset I want to know which values are stored in a specific column/fieldname of all stored lines.
SELECT COUNT(DISTINCT columnname) AS Total FROM yourtable
Related
I have this query in php. It's an insert select copying from table2, but I need to get the IDs of the newly created rows and store them into an array. Here is my code:
$sql = "INSERT INTO table1 SELECT distinct * from table2";
$db->query($sql);
I could revert the flow starting with a select on table2 and making all single inserts but it would slow down the script on a big table. Ideas?
You could lock the table, insert the rows, and get the ID of the last item inserted, and then unlock; that way you know that the IDs will be contiguous as no other concurrent user could have changed them. Locking and unlocking is something you want to use with caution though.
An alternative approach could be to use one of the columns in the table - either an 'updated' datetime column, or an insert-id column (for which you put in a value that will be the same across all of your rows.)
That way you can do a subsequent SELECT of the IDs back out of the database matching either the updated time or your chosen insert ID.
How to get duplicate rows containing same string (crn) from a table & crn can contain multiple values too? I got result only for single valued crn.
I wrote a MySQL Query:
SELECT * FROM event_participation
WHERE event_id=15 AND crn IN (SELECT crn FROM event_participation GROUP BY crn HAVING COUNT(*) > 1) ORDER BY crn;
This query can return rows what I expect for but only for single valued crn as shown in second figure.
The first figure shows duplicate rows while fetching all rows from a table but my MySQl query cannot return these duplicate rows containing one of a crn 720286 as in second figure.
Please help me to display those two rows in second figure too.
GitHub Issue: https://github.com/ErSKS/KhEC/issues/10
I am writing a complex MySQL query. My actual query is more complex than I mentioned below.
I have a table named example and columns are id, name, option_1, option_2 . Of course id column is PK . I want to retrieve like this:
SELECT `id`,`name`,count(`option_1`),count(`option_2`)
My problem is I want to use "GROUP BY `id`" for count(`option_1`) and "GROUP BY `name`" for count(`option_2`) respectively. Now I have to break down it into multiple code in my php code.
How can I achieve what I want in a single query?
What you're asking for doesn't make a ton of sense. You want option 1 grouped by id and option 2 grouped by name, and you want to show all four columns in one result set.
First of all, if id is a primary key, then the count will just be the number of rows in the table since there will be no duplicate ids.
Second, even if id wasn't a primary key, and there were duplicate values, the grouping is different, so the counts represented in your result set would be grouped incorrectly.
Given that id is a primary key, perhaps your intention is actually to get a total count of the rows in the table. Perhaps this query would suit your needs?
SELECT
name,
COUNT(option_2) AS options
FROM
example
GROUP BY
name
UNION ALL
SELECT
'Total' AS name,
COUNT(*) AS options
FROM
example
This should get you a count of all the option_2 values, grouped by name, with the final row having a name of 'Total' and the count being the total number of rows in the table.
Aside from that, I'm not sure you're going to find the answer you're looking for due to the problems you would encounter matching up groupings.
So my problem is that I want to select rows where league_id is equal to something, but I want to store multiple values in one cell.
So this is may tabel and as you can see, I have two values in one cell.
I want to select rwo from DB where league_id is equal 2.
The best way would be to normalize this data and store the leagues in a separate table, but if you are not able or willing to do that, you can use the FIND_IN_SET function in MySQL.
SELECT *
FROM YourTable t
WHERE
FIND_IN_SET('2', t.league_id) > 0
I have column in mysql table (my_sale_time) of type timestamp....infact the rows value look like this
2010-12-01 14:38:07
2010-12-01 17:14:18
...
so what i need mysql query to delete those value whose date is repeated multiple times in table.....as i mentioned in sample sale_time_value.....only date is repeated with same value but time is different....so i want to get all rows, date is repeated multiple times and delete duplicate dates
The basic principle of deleting duplicate rows:
CREATE TEMPORARY TABLE tmptbl AS SELECT DISTINCT * FROM my_sale_time;
DELETE FROM my_sale_time;
INSERT INTO my_sale_time SELECT * FROM tmptbl;
You may have to specify columns and WHERE clauses (I didn't really understand your criteria).
And of course you should test-run it on a development server and don't forget to run it as a single transaction with locked tables.
If you have an auto_increment field, use this:
DELETE FROM
`mytable`
WHERE
`my_auto_increment_field` NOT IN (
SELECT
MAX(`my_auto_increment_field`)
GROUP BY
`my_sale_time`
);