Selecting rows on Unique Pairs of columns from mysql Table - php

I have a Table table .
Now this has three columns table_id,content_id,content_type
What i want is to SELECT rows based on unique pairs of columns.
Say For example i have rows like this-->
id|content_id|content_type|
1 1 shirt
2 1 trouser
3 4 skirt
4 4 shirt
5 3 trouser
6 5 jeans
7 1 trouser
8 5 jeans
I want a query which selects Rows with id->1,2,3,4,5,6.
Rows with id->7,8 are not to be selected
Therefore it concludes that i dont want to select complete Duplicates of Rows

You can use a self join to pick a minimum row per group
select t.* from
test t
join (
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
) t1
on(t.id = t1.id
and t.content_id = t1.content_id
and t.content_type = t1.content_type)
Demo
or if there are only these 3 columns in your table then you can simply use min()
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
Demo

This is mysql-specific : If you use the GROUP BY function without using aggregate functions, the group by clause will behave as distinct, and pick up the first distinct row.
select id, content_id, content_type from test group by content_id, content_type order by id
Demo

Related

Mysql query help to get users count from table area wise

I have a table which is having some report info. In that table I have userid and areaname .
Now my requirement is to get the count of user's list based on area
For example table will have
userid | areaname
-----------------
1 |area 1
1 |area 1
2 |area2
2 |area 2
2 |area2
3 |area1
3 |area1
4 |area3
5 |area2
---------------
Result must be
area1 2users
area2 2users
area3 1user
what is the mysql query to achieve this?
Use the Aggregate function COUNT() to get the number of users and GROUP BY to get the count based on areaname
Use DISTINCT for unique values
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname;
SELECT count(userid),area FROM YOURTABLENAME GROUP BY area;
I see, you can have dublicate entries ad well:
So I would make a sub Select
SELECT sum(partly_sum), area
FROM (
SELECT
userid, area , count(*) as partly_sum
FROM
_YOUR_TABLE_NAME_
WHERE 1
GROUP BY area, userid
) as a_bad_sub_query
WHERE 1
GROUP BY area
Regards
You can use distinct to avoid dupe entries
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname

select all from two tables with sum cost

I try to select all from 2 tables in mysql database with the sum of total cost in the second table
i have a table name tbl_project contain
db_id db_projectname and other column
1 test
2 test2
3 test3
second table name tbl_activities contain
db_id db_projectname db_totalcost
1 test 200
2 test 300
3 test2 800
the out put i want is
test 500
test2 800
test3
i try this query but it didn't give me that result
select tbl_project.db_id, tbl_project.db_projectname,tbl_project.db_location,tbl_project.db_client,tbl_project.db_transferredto,tbl_project.db_psd,tbl_project.db_pdd,tbl_project.db_duration,tbl_project.db_past,tbl_project.db_padd,tbl_project.db_aduration,tbl_project.db_percent,tbl_project.db_pnote,tbl_project.db_user,tbl_project.db_cpercentage,tbl_project.db_epercentage,tbl_project.db_mpercentage,tbl_project.db_status,tbl_project.db_offer,tbl_project.db_sheet,tbl_project.db_invoice,tbl_project.db_po,sum(tbl_activities.db_totalcost) as total_cost from tbl_project,tbl_activities where
tbl_project.db_projectname=tbl_activities.db_projectname
it give me
test but sum of another project and only one project not all
You needed to use LEFT JOIN & GROUP BY
SELECT
tbl_project.db_id,
tbl_project.db_projectname,
tbl_project.db_location,
tbl_project.db_client,
tbl_project.db_transferredto,
tbl_project.db_psd,
tbl_project.db_pdd,
tbl_project.db_duration,
tbl_project.db_past,
tbl_project.db_padd,
tbl_project.db_aduration,
tbl_project.db_percent,
tbl_project.db_pnote,
tbl_project.db_user,
tbl_project.db_cpercentage,
tbl_project.db_epercentage,
tbl_project.db_mpercentage,
tbl_project.db_status,
tbl_project.db_offer,
tbl_project.db_sheet,
tbl_project.db_invoice,
tbl_project.db_po,
sum(
tbl_activities.db_totalcost
) AS total_cost
FROM
tbl_project
LEFT JOIN tbl_activities ON tbl_project.db_projectname = tbl_activities.db_projectname
GROUP BY tbl_project.db_id
Note:
Using aggregate function (e.g. SUM,COUNT..) without GROUP BY collapses the result set into a single row.
try this;
SELECT
db_projectname.db_projectname,SUM(tbl_activities.db_totalcost) AS total_cost
FROM db_projectname
LEFT JOIN tbl_activities
ON
db_projectname.db_projectname = tbl_activities.db_projectname
GROUP BY db_projectname.db_projectname

Fetch data from mapping table if the condition matches in multiple rows

product_id property_id
1 2
1 5
2 2
3 5
I have a mapping table as above. I want to get only product with id =1 if product_id in (2,5). i.e. I want to fetch data if the table contains both 2,5 not the data if it is with property_id only 2 or 5
select group_concat(distinct product_id) product_ids from table where property_id in (2,5)
UPDATE:
The property_id in can be property_id in(2,5,....). I get output from form as 2,5,.... and so on. Its not just for the single case. I just want the output if the condition in property_id in matches the whole series.
This how it could be done
select
product_id from
table_name
where property_id in (2,5)
group by product_id
having count(*) = 2
All you need to change having count(*) = 2 to the number of items inside IN() , right now its 2 and if you are looking at 3 property id then it will be 3 and so on.
select distinct a.product_id
from table a, table b
where a.product_id = b.product_id
and a.property_id = 2
and b.property_id = 5

Merge results from two different tables with different column number, ORDER them by same key

I have two tables:
Table A: ID Cars Planes Num
1 3 5 2
2 8 44 1
3 7 23 6
4 6 2 7
Table B: ID Horses Dogs Cats Elefants Num
1 3 5 2 3 3
2 8 44 1 22 4
3 7 23 4 14 8
4 6 2 3 15 5
What I need to do: I need to get all results from both tables and sort them by the "Num" Column, where the "number" actually is unique for each result from both rows.
Is it even possible to "merge" those two tables and order them by "num" or should I just get each table separately ordered and do two loops checking always for the next num jumping between tables?
Thanks
you can merge them like that with UNION .
try this:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
DEMO HERE
EDIT:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
DEmo with other columns
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1

PHP MySQL Joins

Is there a way I could somehow do the following?
Table ONE
id c_id type
-------------------
1 1 7
2 2 7
3 3 5
4 4 7
Table TWO
id title live
-------------------
1 row1 1
2 row2 0
3 row3 0
4 row4 1
The c_id column links the data from table ONE to table TWO. Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2".
I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1.
Here's how I thought I'd do it, but this doesn't seem to work:
SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5
I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1.
You were close... try using an implicit join:
SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5
select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 5

Categories