I have to display result with select multiple column but distinct on only one column i have also used following queries and also refereed below link.
Query One.
select category , offer_id, store_image from `tbl_coupan_offer` where
`offer_id` in (Select max(`offer_id`) FROM `tbl_coupan_offer` group by `category`)
Above queries return all record with including duplicate
I want only display distinct category not repeated
Following is image
On image you can see Travel and Accessorises repeated
Second query
SELECT DISTINCT `category`,`offer_id`,`store_image` FROM `tbl_coupan_offer`
I also refereed this link Select all columns from rows distinct on one column
Remove offer_id from the select:
SELECT DISTINCT category
FROM tbl_coupon_offer;
If you want one offer_id, then use GROUP BY:
SELECT category, MAX(offer_id)
FROM tbl_coupon_offer
GROUP BY category;
Like Gordon said but as a subquery to get the appropriate image.
SELECT DISTINCT q.category, ch_offer_id, store_img
FROM
(
SELECT category, MAX(offer_id) ch_offer_id
FROM tbl_coupan_offer
GROUP BY category
) q
JOIN tbl_coupan_offer
ON q.ch_offer_id=tbl_coupan_offer.offer_id
SELECT category , MAX(`offer_id`) , store_image
FROM `tbl_coupan_offer`
GROUP BY `category`
If multiple data coming then just add DISTINCT after SELECT
Check your database engine type. It is "InnoDB" or not. If your database and table both existed and if you find this error #1146 Table xxx doesn't exist. Then read more about database engine.
If all is right, then query of Gordon is right.
SELECT `category`, `offer_id`, `store_image`
FROM `tbl_coupon_offer`
GROUP BY `category`
Related
I have the following database and want to delete the red ones because they are doubouled. So I have to check every row if another row is matching by pid, price, price_old, link and shop.
But how can I check that and how can I delete it then?
Maybe an easier way would be to generate a id from the values inside each row. So if the values inside a row would be equal also the id would be equal and who have only one value to compare with the other id's.
Is that a better way? - If yes, how can I do that?
Greetings!
Do the fact you have no way for get thi distinct row you could add uniqie id using
ALTER TABLE my_table
ADD id int NOT NULL AUTO_INCREMENT
Once done you could use not in where the id are not the min grouped by the value you need for define the duplication
delete from my_table
where id NOT in ( select min(id) from my_table
group by shop, link
)
The simplest way is to run a distinct query:
select distinct pid, price, price_old, link, shop
from t;
You can create a new table using into. That is the simplest way. Because all columns are the same, MySQL doesn't offer a simple method to delete duplicate rows (while leaving one of them).
However, it is possible that your current results are generated by a query. If so, you can just add select distinct to the query. However, it would be better to fix the query so it doesn't generate duplicates. If this is the case, then ask another question with sample data, desired results (as text, not an image), and the query you are currently using.
Test this first on a test table:
DELETE t1
FROM t t1, t t2
WHERE t1.id > t2.id AND t1.price = t2.price
AND t1.link = t2.link AND t1.shop = t2.shop
AND t1.price_old = t2.price_old;
Basically you are removing the one with the highest ID if those parameters are equal
select * from
(select pid, price, price_old, link ,
row_number() over(partition by pid, price, price_old, link, shop order by pid) as rank
from my_table) temp
where temp.rank = 1
This Query will group by all the columns first and rank them. Duplicate rows will have rank > 1. It does not matter we take first or second row as both are copy of each other. We just take rows with rank 1. Rows that are not duplicate will also be having rank 1 and hence won't be neglected.
One more way to this is by using union.
select * from my_table UNION select * from my_table
I have used mysql DISTNCT keyword for one of my mySQL query for avoid duplication of data displaying on search box.
But when I add multiple column names on SELECT it doesn't work.
Please advice how to proceed.
$query = "
SELECT * FROM (
SELECT DISTINCT b.title,b.id, b.metakey
FROM categories b
WHERE b.title like '%".$searchc."%' AND b.parent_id BETWEEN 84 AND 107 AND b.level=3 ORDER BY LOCATE('".$searchc."', REPLACE(b.title, ' ', '')), b.title
) CLASS_CAT
UNION ALL
SELECT * FROM (
SELECT DISTINCT a.title, a.id, a.title as metakey
FROM content a join
categories b
on a.categories_id = b.id
WHERE REPLACE(a.title, ' ', '') like '%".$searchc."%'
AND b.parent_id BETWEEN 84 AND 107 AND b.level=3
) CLASS_ITEM
";
SELECT DISTINCT will remove duplicates from the one SELECT statement. UNION ALL directs the system to not look for duplicates between the two sets you are combining (each SELECT).
Use UNION (without ALL) instead. Note that removing the check for duplicates is faster, so if you know a set you're querying is unique, skip the dup check.
Also note that by row duplicates I'm referring to every column in every row. If any column makes a row unique it will appear in the result set. If you only want some columns to be unique you'll need to GROUP BY and aggregate the other columns (e.g. GROUP_CONCAT) or use additional queries to get other related data.
SELECT a.delivery_date,
a.delivery_hour,
a.price as EX-ANTE,
FROM mms_realtime_dispatch_prices_report a
UNION ALL
SELECT b.delivery_date,
b.delivery_hour,
b.price as EX-POST,
FROM mms_realtime_dispatch_prices_report b
UNION ALL
SELECT c.region,
c.dem_rtdel,
c.date,
FROM pub_demand_lwap c;
UNION ALL
SELECT region,
report,
hour,
SUM(q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11)
FROM pub_markets_bids_and_offers
WHERE delivery date=03/16/2011
GROUP BY hour
help! need to combine this four table into one new table no duplicate data
Can you help me in combining this four tables into one table. this is the first time i encounter this. I really need a help :(
In SQL server union works only if there is same number and type of columns return by query.
You need to get your Union query right 1st I can see so many things wrong with Query At the moment,
Your number of columns retunred by each select arent same, Last query is returning 4 columns and other 3,
You are Aliasing Columns in 2nd query, it will not have any effect as Only the Column Names from very first select statement are visible in the result set.
Guessing from the Column names you have Different Data Types that
you are trying to UNION. Datatypes Returned from all selects that you are using in UNION should return the same datatypes e.g
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name1
UNION ALL
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name2
UNION ALL
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name3
and so on....
Once you have met all these condition then you can do something like this to eliminate duplicate rows from you result set
;with CTE
AS
(
SELECT ID_Column, rn = ROW_NUMBER() OVER (PARTITION BY Column1, Column2, Column3... ORDER BY ID ASC)
FROM ( -- All of your UNION ALL Statements Can go here --)q
)
DELETE FROM CTE
WHERE rn = 1
I want to write a query to search in multiple tables (news, articles, projects, files)
While searching I found this
SELECT title, post FROM news WHERE MATCH (title,post) AGAINST ('press');
I tested it and it's working, but i failed to extend this to multiple tables.
How to write one query that return me search results for multiple tables ?
One method is to use a union:
SELECT title, post FROM news WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM articles WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM projects WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM files WHERE MATCH (title,post) AGAINST ('press')
This now essentially becomes one pseudo table with all of the records merging into one dataset.
This is one way to do it. I'm not sure what Match does, but in the where you can also your Match function
Select Table1.Title, Table2.Post FROM Table1, Table2 WHERE Table1.Title = 'press' AND Table2.Title = 'press'
This query will give you the Title and the Post of the 2 tables that have both have press in it.
Have a look on this query , you may use like this -
SELECT *
FROM news t1, articles t2, projects t3, files t4 WHERE MATCH ( t1.title, t1.post)
AGAINST ('press') or Match(t2.title,t2.post) AGAINST('press')
and set all column you want to search set in MATCH() function.
You could try this, it may helpful for you.
SELECT DISTINCT business.name AS businessname
,business.description AS description
FROM business
, category
, sub_categories
WHERE business.cityID = '$city'
AND (category.name LIKE '%$name%'
OR sub_categories.name LIKE '%$name%')
AND business.status = 0
Pls the above SQL code is suppose to search a set of two tables the ones in the bracket and return the result, but for some reason, it's not doing so. What am i doing wrong?
Thank You.
Your query would produce a cartesian product. Depending on the size of your tables that could take a considerable amount of time.
Based on your clarification I'd use a subquery to check for matching categories, this way you don't have to use distinct in your query as it would only return each business once. I also suggest you to start with a decent SQL tutorial.
SELECT name AS businessname
,description AS description
FROM business
WHERE cityID = '$city'
AND status = 0
AND ( categoryID in (select id from category where name like '%$name%')
or subcategoryID in (select id from sub_categories where name like '%$name%')
)
Two things come to mind:
You are not joining any of the three tables together. Consider adding a few LEFT JOIN clauses.
You are selecting columns from only one table. If you wanted columns from other tables, you should add them to your SELECT clause.