Mysql - Select distinct column with column id query is not working - mysql

i have few duplicate rows in my mysql table which belongs to different different categories , my table structure is like this .
sample_table
-----------------------
id | title | category
-----------------------
1 |item 1 | category 1
2 |item 1 | category 2
3 |item 1 | category 3
4 |item 2 | category 1
5 |item 2 | category 3
my problem is I want distinct title column from my table along with their id ,I tried select distinct title from sample_table,it returns 2 rows,
but if i try select distinct title , id from sample_table it returns all 5 rows.
Now my problem is I want distinct title with their id's from table i.e 2 rows with their id's.

use group by
select id,title from sample_table group by title

Use instead select with a group by statement like that :
select title, group_concat(id separator ' ') as ids
from sample_table
group by title;
it will return 2 rows like that :
title ids
item1 1 2 3
item2 4 5

Related

How can I Count in multiple table?

I have some problem to get the ID which use on another table row. I have three tables. One is category, articles, video. In articles and video there is a column which have category ID. This is the example:
Table Categories :
id | category_name
------------------
1 | News
2 | Sports
3 | Art
4 | Horror
Table Articles :
id | category_id | title
----------------------------------
1 | 1 | title content 1
2 | 1 | title content 2
3 | 3 | title content 3
4 | 3 | title content 4
5 | 2 | title content 5
Table Video :
id | category_id | video_title
------------------------------
1 | 1 | video title 1
2 | 2 | video title 2
3 | 3 | video title 3
I want to get each category ID already use what time in two other databases. Like this :
Category ID 1 is use 3 times
Category ID 2 is use 2 times
Category ID 3 is use 3 times
Category ID 4 is use 0 times
What query do I need to use so I can get all data like that ? Please anyone knows could help me. Thanks in advance.
First you need to UNION ALL articles table and video table be a subquery, then use Outer join and COUNT function.
SELECT Concat('Category ID ', c.id, ' is use ', Count(t.category_id ), ' times')
FROM categories c
LEFT JOIN (SELECT category_id
FROM articles
UNION ALL
SELECT category_id
FROM video) t
ON c.id = t.category_id
GROUP BY c.id
SQLFIDDLE:http://sqlfiddle.com/#!9/92cbd0e/12
[Results]:
| Concat('Category ID ', t.id, ' is use ', t.cnt, ' times') |
|------------------------------------------------------------|
| Category ID 1 is use 3 times |
| Category ID 2 is use 2 times |
| Category ID 3 is use 3 times |
| Category ID 4 is use 0 times |
NOTE
COUNT function does not count numbers if the column value encounters null
For example Here is a sample script.
CREATE TABLE T(
col int
);
INSERT INTO T VALUES (NULL);
INSERT INTO T VALUES (1);
SELECT COUNT(col) FROM t; -- RESULT = 1
SELECT COUNT(*) FROM t; --RESULT = 2
sample sqlfiddle:http://sqlfiddle.com/#!9/e2bba7/2
You can use, for example, this query:
Select category_id, count(1) - 1
From (
Select category_id From video
Union All Select category_id From articles
Union All Select id From Categories)
Group By category_id

MySQL Error to selecting data

I have two column one column associated with another...
Table:base_data
id |---name----|-----des
1 | some name1 | The description1
2 | some name2 | The description2
Table: photos
id |---p_id----|-----photo
1 | 1 | img1s.jpg
2 | 1 | img1w.jpg
3 | 2 | img2.jpg
4 | 2 | img14.jpg
5 | 2 | img15.jpg
I want to select all data from table 1(base_data) and one row from associated row from photos: table how can I do that ????
I don't want to select by greatest n per group I want to select all data from the first table and only one row of the second table which matches with the first table row id, just first match not other.
The Result I want...
id |---name----|---des----|---p_id----|---photo----|
1 | some name |the des..1| 1 | img1s.jpg|
2 | some name |the des..2| 2 | img2.jpg|
I suppose you want to associate base_data with the first photo taken, which should be the one with the lowest photos.id. In MySQL, you could write this as follows: Create an intermediate query which gives - for any p_id - the corresponding record with the lowest id. Then, left join base_data with this intermediate query result. Hope there are not to many typos in it :-) :
select b.id, p2.photo
from base_data b left join
(select p.photo, p.p_id, min(id) from photos p group by p.p_id) p2 on b.id = p2.p_id
If you want the alphanumerically lowest photo name, in MySQL you can do this:
select
t1.*,
t2.photo
from
base_data as t1
left join (
select
p_id,
min(photo) as photo
from
photos
group by
p_id
) as t2 on t2.p_id = t1.id;

SQL: Count distinct row values in table

I have a similar table to this in SQL:
id |tag | entryID
---+----+--------
1 |foo | 0
2 |foo | 0
3 |bar | 3
5 |bar | 3
6 |foo | 3
7 |foo | 3
I want to run a query to count distinct rows in the table (with the id column dropped). The result should look like this (or a transpose of this table):
(tag=foo, entryID=0) | (tag=foo, entryID=3) | (tag=bar, entryID=3)
---------------------+----------------------+---------------------
2 | 2 | 2
What should this query look like?
Note: The values in each of the columns are not known beforehand.
You can do this using conditional aggregation:
select sum(tag = 'foo' and entryId = 0) as "tag=foo, entryID=0",
sum(tag = 'foo' and entryId = 3) as "tag=foo, entryID=3",
sum(tag = 'bar' and entryId = 3) as "tag=bar, entryID=0"
from t;
However, the normal method is to put the counts in rows, not columns:
select tag, entryId, count(*)
from t
group by tag, entryId;
The rows are much more versatile, because you don't have to list out every combination you might want to match.

my sql order by with values

I have a list of products which have an ID column as primary key, and category id which is a foreign key.. I want to overthem by a certain way.
ID | Name | CategoryID
----------------------------
1 | one | 1
2 | two | 2
3 | three | 1
4 | four | 3
5 | five | 5
6 | six | 4
7 | seven | 2
8 | eight | 1
if the above is my table. I want to get them in an SQL like the folowing
if want order these products in a certain way where all the products of category 5 needs to be
appearing first I am running a query like this.
SELECT * FROM Product ORDER BY CategoryID IN (5), ID
this does the job well.
but now i am in need to show the category id 5 first and then category id 2 first and the rest
if I try
SELECT * FROM Product ORDER BY CategoryID IN (5), CategoryID IN (2), ID ASC
that doesnt work.
any suggestions
You can use MySQL's FIELD() function:
SELECT * FROM Product ORDER BY FIELD(CategoryID, 2, 5) DESC
See it on sqlfiddle.
Try this way:
SELECT * FROM Product
ORDER BY
case CategoryID
when 5 then 0
when 2 then 1
else 2
end
, ID

How to fetch count of every classid from DB table in Mysql see below for detail

I have this table structure
| id | classid | contextid |
----------------------------
1 2 2
2 3 1
3 2 1
4 3 1
5 1 2
5 1 4
How to fetch count of every classid from DB table in Mysql I need a select query for it?
Count of every classid... do you mean something like this:
SELECT classid, COUNT(*) as cnt FROM tbl_name GROUP BY classid
SELECT classid, COUNT(*) FROM table1 GROUP BY classid;
SELECT classid, COUNT(classid) classid_count FROM table_name GROUP BY classid;