I'm trying to get the correlated value in a table from a mysql query which looks like:
select min(c), d
from example
table example looks like:
c | d
----------------
'1,99', '30,99'
'5,99', '8,46'
'9,99', '14,99'
'11,79', '17,24'
'12,99', '19,44'
'15,99', '22,44'
'22,49', '34,48'
Given result:
1,99 & 34,48
Expected result:
1,99 & 30,99
What I want is the correlated value from the min(c) in this case '30,99'. How to do that?
You can do it with a sub query:
SELECT * FROM example
WHERE c = (SELECT min(c) from example)
EDIT: if there's more then 1 records the answer the condition, then you need to decide which one you wanna pick.
SELECT * FROM example
WHERE c = (SELECT min(c) from example)
ORDER BY d
LIMIT 1
This will take the one with the smallest d value.
To take the biggest, add DESC after the order by d in the query.
You need to use subquery:
select e.*
from (select min(c) as c from example) x
join example e
on x.c = e.c;
Related
My query has multiple column being selected but now i want when no returned I want some dummy value to be returned.
For example in the following query:
SELECT a.abc, b.def, c.ghi
FROM comp a, damp b, champ c, omp d
WHERE a.id=b.id
and b.id=c.id
and c.id= d.id
ORDER BY a.abc desc
if there is no row returned I wanted to display atleast one column with some value, can somebody suggest me how can I achieve this.
I have already gone through some suggestion but none worked. Any help would be appreciated.
In oracle you could do something like this:
WITH mybigselect AS
(
SELECT a.abc, b.def, c.ghi
FROM comp a
JOIN damp b ON a.id = b.id
JOIN champ c ON b.id = c.id
JOIN omp d ON c.id = d.id
ORDER BY a.abc desc
)
SELECT * FROM mybigselect
UNION ALL
SELECT 'Nothing found', NULL, NULL FROM mybigselect
WHERE NOT EXISTS (SELECT * FROM mybigselect)
Note 1: that both rows in the UNION ALL needs to return columns of the same datatype. You can't return a number in the first column of SELECT * FROM mybigselect and "nothing found" in the query after UNION ALL
Note 2: rewrote the query using ANSI-JOIN style syntax.
I would recommend:
WITH cte as (
SELECT a.abc, b.def, c.ghi
FROM comp do JOIN
damp d
ON d.id = co.id JOIN
champ c
ON ch.id = d.id
omp o
ON o.id = ch.id
)
SELECT *
FROM cte
UNION ALL
SELECT 'Some value', NULL, NLL
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM cte)
ORDER BY abc DESC;
Notes:
The value has to be compatible with the type of the column. So, if abc is not a string, then 'Some value' is not appropriate. You haven't provided enough information to determine what value should be in which column.
The ORDER BY should be in the outermost query, not the CTE.
Never use comas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.
This version uses meaningful table aliases (table name abbreviations) rather than arbitrary letters.
I am not sure if my method is possible, but i'm trying to do a group_concat on a select statement that concats 2 fields. I get the error: Subquery returns more than 1 row each time. Can anyone help me as to a solution, or better way around this.
select t.recnum, (select group_concat((select concat(b.origtests,'^', d.name) as testing
from order_origtests b
join profile c on c.code = b.origtests
join department d on d.recnum = c.dept
)))
FROM order_ t
You don't put SELECT inside GROUP_CONCAT. It should be
select t.recnum, (
select group_concat(concat(b.origtests,'^', d.name))
from order_origtests b
join profile c on c.code = b.origtests
join department d on d.recnum = c.dept
) AS testing
FROM order_ t
Note that your subquery isn't correlated to anything in t, so you'll get the same testing column for every recnum.
Suppose I have two tables in one to many relationship.
And, I want to select columns from each main record along with the first record from a related table.
I tried some ways but it just doesn't go...
Here I end up with this SQL fiddle:
http://sqlfiddle.com/#!2/39fdb/3
The problem there is that it just cannot reference a.ID from a subselect.
This does not work, of course, but it's just all I could think of
select a.*,b.* from event a left join
(select * from event_pictures where a.ID=article limit 1)
b on a.ID=b.article;
Any ideas on how to fix it?
No, you can't reference a.ID in a subselect that is joined to a. You can do the following, but you better supply an ordering. Otherwise, there is no "first" row. A (more or less) random row from table b will be selected:
select a.*, b.*
from event a
left join event_pictures b
on b.PK = --- the PRIMARY KEY
( select bb.PK --- of event_pictures
from event_pictures bb
where a.ID = bb.article
ORDER BY bb.something
limit 1
) ;
If you don't care which image gets returned for an article, you can select the MIN or MAX image grouped by article (rather than doing a LIMIT 1) in your subquery
SQL Fiddle
You could use min or max as suggested already:
select
e.*,
(
select min(ep.img)
from event_pictures as ep
where ep2.article = e.article
) as img
from
event as e
If you want img based on highest ID:
select
e.*,
(
select ep2.img
from event_pictures as ep2
where ep2.ID = last_ep.last_ID
) as img
from
event as e inner join -- could be a left join if necessary
(
select ep.article, max(ep.ID) as last_ID
from event_pictures as ep
group by ep.article
) as last_ep
on last_ep.article = e.ID
Neither approach requires the use of limit.
Here is one way to do it:
select e.*, ep.*
from (select e.*,
(select article
from event_pictures ep
where ep.article = e.id
order by rand()
limit 1
) as eparticle
from event e
) e left join
event_pictures ep
on e.eparticle = ep.article
The subquery finds one randome "article". The information for this is then joined in.
I got 2 tables: job and job_working_time
job: id (Increment, Index, Unique)
job_working_time: job_id(allow multiple), property_working_time
This SQL query returns the ids with multiple values or duplicates. It helps, but it doesn't fix my problem:
SELECT a.id AS id, count( b.job_id ) AS cnt, b.property_working_time AS value
FROM job a
INNER JOIN job_working_time b ON a.id = b.job_id
GROUP BY b.job_id
HAVING cnt >1
I want to remove duplicates, only for ids with duplicates, e.g: l = leave, r = remove
1 - 1 (l)
1 - 1 (r)
1 - 2 (l)
1 - 3 (l)
1 - 1 (r)
2 - 1 (l)
2 - 1 (r)
2 - 2 (l)
Thanks in advance
[Later edit]
One thing you should consider:
Anyway, when I want to delete the ID, will remove all values for it. So, the idea is to keep all the values for that id, so later I can add without duplicates. That's why it's so important to retrieve, both the id and the value, but not duplicate content. The SQL should return all the (l) values from the above e.g. list.
Since your example looks like you don't want any row be same
DISTINCT will work for you
it select completely diffirent rows
SELECT DISTINCT a.id AS id, count( b.job_id ) AS cnt, b.property_working_time AS value
FROM job a
INNER JOIN job_working_time b ON a.id = b.job_id
GROUP BY b.job_id
HAVING cnt >1
Edit:
Example added
https://data.stackexchange.com/stackoverflow/q/119267/
First/ simple Query:
SELECT id, field, count(*) c from ... group by field having c >1
Second Query, modifed for get IDs to delete:
SELECT instr(s,mid(s,',')+1) from (
SELECT group_concat(id) s, count(*) c from ... group by field having c >1)z
and the end:
Delete FROM ... WHERE id in ( ...)
looks ugly, but fast (Mid function performance is faster than not in (...)).
I have a table like so:
id attr
1 A
2 A
3 C
4 C
5 D
6 F
I want a count of all the A's B's (but not the C's D's, etc..) Note that my table has zero B's.
So I want a command like this:
SELECT count(attr=A, attr=B) FROM table;
or this:
SELECT count(*) FROM table GROUP_BY attr IN (A, B);
and get:
attr count
A 2
B 0
My actual table has about a thousand attrs. I want to do a group_by-ish thing on maybe a hundred or so of them. It's important that i get the count of zero for certain attrs and that i can correlate the attr to the count.
I know this is a basic question and I'm not surprised if this has been asked before. I searched.. But my apologies anyway..
SELECT T.attr,
COUNT(Y.id)
FROM (SELECT 'A' AS attr
UNION ALL
SELECT 'B') AS T
LEFT JOIN YourTable Y
ON Y.attr = T.attr
GROUP BY T.attr
This should work for you.
SELECT T.Attr,Count(A.ID)
FROM (
SELECT CONVERT('A',char) AS Attr
UNION
SELECT CONVERT('B',char) AS Attr
) AS T
LEFT JOIN MyTable AS A
ON T.Attr=MyTable.Attr
GROUP BY T.Attr
ORDER BY T.Attr;
The Convert part may not be necessary but was necessary in my testing.