I have this data
+----+--------+---------------------------------+
| id | fromid | message |
+----+--------+---------------------------------+
| 1 | 1213 | this is just an example |
+----+--------+---------------------------------+
| 2 | 1992 | other random message |
+----+--------+---------------------------------+
| 3 | 1364 | example number three |
+----+--------+---------------------------------+
I need to search data where fromid='1992' or message LIKE '%example%'
if there is any result where fromid matches 1992 value, return this result, and ignore second condition
but if there is no result from first condition (fromid='1992'), return the result from second condition
can I do that on single query?
One way to do it is using a marker, and then limiting the results to the first row. An example using UNION:-
SELECT 1 AS match_type, my_table.*
from my_table
where fromid = 1992
UNION
SELECT 2 AS match_type, my_table.*
from my_table
WHERE message like '%example%'
ORDER BY match_type
LIMIT 1
Down side of this is that it still forces a relatively slow non indexed query using LIKE with a leading wildcard.
The following is an alternative. In most circumstance it would be pointless, but should the check on id be expected to bring back a record almost always, and the check on the LIKE just a very occasional condition (with only a single record ever returned) then it might be worthwhile as it would avoid having the do the slow LIKE condition searched unless necessary.
SELECT COALESCE(id, SELECT MAX(id) from my_table WHERE message like '%example%') AS id,
COALESCE(fromid, SELECT MAX(formed) from my_table WHERE message like '%example%') AS formed,
COALESCE(message, SELECT MAX(message) from my_table WHERE message like '%example%') AS message
FROM
(
SELECT id, fromid, message
FROM (SELECT 1 AS a_cnt) sub0
LEFT OUTER JOIN
(
SELECT id, fromid, message
from my_table
where fromid = 1992
) sub1
) sub2
Related
The solution to the topic is evading me.
I have a table looking like (beyond other fields that have nothing to do with my question):
NAME,CARDNUMBER,MEMBERTYPE
Now, I want a view that shows rows where the cardnumber AND membertype is identical. Both of these fields are integers. Name is VARCHAR. Name is not unique, and duplicate cardnumber, membertype should show for the same name, as well.
I.e. if the following was the table:
JOHN | 324 | 2
PETER | 642 | 1
MARK | 324 | 2
DIANNA | 753 | 2
SPIDERMAN | 642 | 1
JAMIE FOXX | 235 | 6
I would want:
JOHN | 324 | 2
MARK | 324 | 2
PETER | 642 | 1
SPIDERMAN | 642 | 1
this could just be sorted by cardnumber to make it useful to humans.
What's the most efficient way of doing this?
What's the most efficient way of doing this?
I believe a JOIN will be more efficient than EXISTS
SELECT t1.* FROM myTable t1
JOIN (
SELECT cardnumber, membertype
FROM myTable
GROUP BY cardnumber, membertype
HAVING COUNT(*) > 1
) t2 ON t1.cardnumber = t2.cardnumber AND t1.membertype = t2.membertype
Query plan: http://www.sqlfiddle.com/#!2/0abe3/1
You can use exists for this:
select *
from yourtable y
where exists (
select 1
from yourtable y2
where y.name <> y2.name
and y.cardnumber = y2.cardnumber
and y.membertype = y2.membertype)
SQL Fiddle Demo
Since you mentioned names can be duplicated, and that a duplicate name still means is a different person and should show up in the result set, we need to use a GROUP BY HAVING COUNT(*) > 1 in order to truly detect dupes. Then join this back to the main table to get your full result list.
Also since from your comments, it sounds like you are wrapping this into a view, you'll need to separate out the subquery.
CREATE VIEW DUP_CARDS
AS
SELECT CARDNUMBER, MEMBERTYPE
FROM mytable t2
GROUP BY CARDNUMBER, MEMBERTYPE
HAVING COUNT(*) > 1
CREATE VIEW DUP_ROWS
AS
SELECT t1.*
FROM mytable AS t1
INNER JOIN DUP_CARDS AS DUP
ON (T1.CARDNUMBER = DUP.CARDNUMBER AND T1.MEMBERTYPE = DUP.MEMBERTYPE )
SQL Fiddle Example
If you just need to know the valuepairs of the 3 fields that are not unique then you could simply do:
SELECT concat(NAME, "|", CARDNUMBER, "|", MEMBERTYPE) AS myIdentifier,
COUNT(*) AS count
FROM myTable
GROUP BY myIdentifier
HAVING count > 1
This will give you all the different pairs of NAME, CARDNUMBER and MEMBERTYPE that are used more than once with a count (how many times they are duplicated). This doesnt give you back the entries, you would have to do that in a second step.
There are a lot of questions posted on stackoverflow that are almost same like my problem but I found no working solution. I have a table message and entries are:
id | Message | Status
1 | Hello | 1
2 | Hiiii | 0
4 | Works | 1
I have another table which gives ids 1,2,3 and I want to find the status of all these entries from message table. What I want is if an id doesn't exist in message table then it should return null for that id if I use IN clause to find all status. I want following result:
id | Status
1 | 1
2 | 0
3 | null
I have been using IN clause but didn't get working output. Then I got a solution on stackoverflow and tried this query
SELECT `id`,`status` FROM ( SELECT 1 AS ID UNION ALL SELECT 2 AS ID UNION ALL SELECT 3) ids LEFT OUTER JOIN message ON ids.ID = message.id
But this query is not giving the expected results. Any help would be much appreciated.
I don't see how your query would work. The column id should be ambiguous in the select (unless your database is case sensitive). Try this:
SELECT ids.ID, m.status
FROM ( SELECT 1 AS ID UNION ALL SELECT 2 AS ID UNION ALL SELECT 3
) ids LEFT OUTER JOIN
message m
ON ids.ID = m.id;
I need to count the number of duplicate emails in a mysql database, but without counting the first one (considered the original). In this table, the query result should be the single value "3" (2 duplicate x#q.com plus 1 duplicate f#q.com).
TABLE
ID | Name | Email
1 | Mike | x#q.com
2 | Peter | p#q.com
3 | Mike | x#q.com
4 | Mike | x#q.com
5 | Frank | f#q.com
6 | Jim | f#q.com
My current query produces not one number, but multiple rows, one per email address regardless of how many duplicates of this email are in the table:
SELECT value, count(lds1.leadid) FROM leads_form_element lds1 LEFT JOIN leads lds2 ON lds1.leadID = lds2.leadID
WHERE lds2.typesID = "31" AND lds1.formElementID = '97'
GROUP BY lds1.value HAVING ( COUNT(lds1.value) > 1 )
It's not one query so I'm not sure if it would work in your case, but you could do one query to select the total number of rows, a second query to select distinct email addresses, and subtract the two. This would give you the total number of duplicates...
select count(*) from someTable;
select count(distinct Email) from someTable;
In fact, I don't know if this will work, but you could try doing it all in one query:
select (count(*)-(count(distinct Email))) from someTable
Like I said, untested, but let me know if it works for you.
Try doing a group by in a sub query and then summing up. Something like:
select sum(tot)
from
(
select email, count(1)-1 as tot
from table
group by email
having count(1) > 1
)
I have a query which looks very much like this:
SELECT * FROM t1 LEFT JOIN t2 ON t1.aNumber = t2.aNumber;
When I try to run it, I get Invalid Operation. Both of the following queries run fine though:
SELECT * FROM t1;
SELECT * FROM t2;
So I'm guessing either the problem is really obvious or really complicated... How can I find the problem in this situation?
For completeness, here is the actual query (Query25) and all the other queries it refers to:
Query25
SELECT *
FROM [EXPORT PRICE AND Desc] LEFT JOIN Export_Union_active ON [EXPORT PRICE AND Desc].MaterialNumber = Export_Union_active.MaterialNumber;
EXPORT PRICE AND Desc
SELECT tblMMMaterials.MaterialNumber, tblMMMaterials.MaterialDescription AS name, Format(tblMMMaterials!SellingPrice,"Standard") AS SellingPrice, dbo_tblMMImages.Notes, tblMMMaterials.MFG, tblMMMaterials.Internet
FROM tblMMMaterials LEFT JOIN dbo_tblMMImages ON tblMMMaterials.MaterialID = dbo_tblMMImages.MaterialID
WHERE (((tblMMMaterials.Internet)=True));
Export_Union_active
select * from Export_Active UNION ALL select * from Export_Inactive;
Export_Active
SELECT tblMMMaterials.MaterialNumber, tblMMMaterials.Discontinued, 1 AS [check], tblMMMaterials.MFG
FROM tblMMMaterials
WHERE (((tblMMMaterials.Discontinued)=0) AND ((tblMMMaterials.MFG)="kheops"));
Export_Inactive
SELECT tblMMMaterials.MaterialNumber, tblMMMaterials.Discontinued, 0 AS [check], tblMMMaterials.MFG
FROM tblMMMaterials
WHERE (((tblMMMaterials.Discontinued)=1) AND ((tblMMMaterials.MFG)="kheops"));
And here are the tables refered to by all these queries (they are much bigger than this, but I trimmed out anything not used in any of the queries):
tblMMMaterials
MaterialID | Number | PK
MaterialNumber | Text
MaterialDescription | Text
SellingPrice | Currency
MFG | Text
Internet | Yes/No
Discontinued | Yes/No
tblMMImages
ImageID | AutoNumer | PK
MaterialID | Number
Notes | Memo
I am attempting to narrow results of an existing complex query based on conditional matches on multiple columns within the returned data set. I'll attempt to simplify the data as much as possible here.
Assume that the following table structure represents the data that my existing complex query has already selected (here ordered by date):
+----+-----------+------+------------+
| id | remote_id | type | date |
+----+-----------+------+------------+
| 1 | 1 | A | 2011-01-01 |
| 3 | 1 | A | 2011-01-07 |
| 5 | 1 | B | 2011-01-07 |
| 4 | 1 | A | 2011-05-01 |
+----+-----------+------+------------+
I need to select from that data set based on the following criteria:
If the pairing of remote_id and type is unique to the set, return the row always
If the pairing of remote_id and type is not unique to the set, take the following action:
Of the sets of rows for which the pairing of remote_id and type are not unique, return only the single row for which date is greatest and still less than or equal to now.
So, if today is 2011-01-10, I'd like the data set returned to be:
+----+-----------+------+------------+
| id | remote_id | type | date |
+----+-----------+------+------------+
| 3 | 1 | A | 2011-01-07 |
| 5 | 1 | B | 2011-01-07 |
+----+-----------+------+------------+
For some reason I'm having no luck wrapping my head around this one. I suspect the answer lies in good application of group by, but I just can't grasp it. Any help is greatly appreciated!
/* Rows with exactly one date - always return regardless of when date occurs */
SELECT id, remote_id, type, date
FROM YourTable
GROUP BY remote_id, type
HAVING COUNT(*) = 1
UNION
/* Rows with more than one date - Return Max date <= NOW */
SELECT yt.id, yt.remote_id, yt.type, yt.date
FROM YourTable yt
INNER JOIN (SELECT remote_id, type, max(date) as maxdate
FROM YourTable
WHERE date <= DATE(NOW())
GROUP BY remote_id, type
HAVING COUNT(*) > 1) sq
ON yt.remote_id = sq.remote_id
AND yt.type = sq.type
AND yt.date = sq.maxdate
The group by clause groups all rows that have identical values of one or more columns together and returns one row in the result set for them. If you use aggregate functions (min, max, sum, avg etc.) that will be applied for each "group".
SELECT id, remote_id, type, max(date)
FROM blah
GROUP BY remote_id, date;
I'm not whore where today's date comes in, but assumed that was part of the complex query that you didn't describe and I assume isn't directly relevant to your question here.
Try this:
SELECT a.*
FROM table a INNER JOIN
(
select remote_id, type, MAX(date) date, COUNT(1) cnt from table
group by remote_id, type
) b
WHERE a.remote_id = b.remote_id,
AND a.type = b.type
AND a.date = b.date
AND ( (b.cnt = 1) OR (b.cnt>1 AND b.date <= DATE(NOW())))
Try this
select id, remote_id, type, MAX(date) from table
group by remote_id, type
Hey Carson! You could try using the "distinct" keyword on those two fields, and in a union you can use Count() along with group by and some operators to pull non-unique (greatest and less-than today) records!