Group by Where(field.id is the same) - mysql

Table header
x---id---x---short_name---------x
| 1 | alcatel |
| 2 | Nexus |
| 3 | ZTE |
x--------x----------------------x
Table Detail
x---id---x---------code---------x---header.id---x
| 1 | XXX | 1 |
| 2 | ZZZ | 2 |
| 3 | ZZZ | 2 |
| 4 | XXX | 3 |
x--------x----------------------x---------------x
And I need to GROUP BY CODE Where field.id is the same, example:
In this case I get 2 rows with same field.id so I need to group by my field code just in case if field.id is the same..
(Very hard to me to explain...)
I need to get this
x---header.id---x--detail.id--x-----short_name----x---detail.code---x
| 1 | 1 | alcatel | XXX |
| 2 | 3 | Nexus | ZZZ |
| 3 | 4 | ZTE | XXX |
x---------------x-------------x------------------x-----------------x
I don't know if this is actually possible to do, but if it is please help me, if it isn't please tell me that this is impossible.
EDIT: my field.id and field.code (the ID and CODE fields) are from a table with a relashion 1 to N called field

select h.id as header_id, max(d.id) as detail_id, h.short_name, d.code
from header h
join detail d on d.`header.id` = h.id
group by header_id, h.short_name, d.code

Try this
SELECT MAX(id) AS id, `field.id`, code
FROM test
GROUP BY code, `field.id`
ORDER BY id ASC
Check SQL Fiddle

Related

MySQL How to Select smth by MAX(id)....WHERE userID = some number GROUP BY smth

I have next table in my DB:
personal_prizes
___________ ___________ _________ __________
| id | userId | specId| grp |
|___________|___________|_________|__________|
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 3 | 2 | 3 | 1 |
| 4 | 2 | 4 | 2 |
| 5 | 1 | 5 | 2 |
| 6 | 1 | 6 | 2 |
| 7 | 2 | 7 | 3 |
| 8 | 1 | 13 | 4 |
|___________|___________|_________|__________|
I need to select specId by max id group by grp.
So I have composed next query
SELECT pp.specId
FROM personal_prizes pp
WHERE pp.specId IN (SELECT MAX(pp1.id)
FROM personal_prizes pp1
WHERE pp1.userId = 1
GROUP BY pp1.grp)
And it's work for my little table. But when I try to implement it for my prod db with personal_prizes > 100,000.
Please help me optimize it
The query you have should work fine. Make sure though that you not only have an index on id (which I suppose is the primary key), but also one on specId.
Just as an alternative, you might try this one:
select group_concat(pp.specId order by pp1.id desc)+0 as result_specId
from personal_prizes pp1
left join personal_prizes pp on pp.specId = pp1.id
where pp1.userId = 1
group by pp1.grp
having result_specId is not null;
The idea here is that the sub query is promoted to the main query, and the specId is retrieved by an outer join. The group_concat aggregation function will list the one of interest as the first. The having clause will exclude the cases where no matching specId was found.
Note that this will only give the same results if the specId field is guaranteed to be non-null.

mysql - Limit a query with left join

I have 2 tables like this:
Table person
id | name
---------
1 | john
2 | mike
3 | carl
4 | keny
5 | anna
Table vehicle
owner | vechicle
----------------
1 | RTA457
3 | GSW684
3 | GKI321
3 | SNE798
5 | YTT662
So, I want to make a query joining both tables, something like this:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner
Getting these results
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
4 | keny | NULL | NULL
5 | anna | 5 | YTT662
Finally, I want to limit it to 3 persons, showing all their vehicles, like this:
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
There is any way to do it?
May help with a subquery
SELECT
*
FROM
(SELECT * FROM person LIMIT 3) t
LEFT JOIN vehicle ON t.id = vehicle.owner
Didn't try it, but something like this:
SELECT * FROM person
LEFT JOIN vehicle ON person.id = vehicle.owner
WHERE person.id IN (SELECT ID FROM PERSON LIMIT 3);
You could simply have your query as such:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner LIMIT 10;
This SO could be handy as well. Hope this helps!

How connect columns based on the value of one field in MYSQL

I have a table looks like this:
|__name___|value__|
|__James__|___6___|
|__Jerry__|___5___|
|__Jerry__|___4___|
|__James__|___3___|
|__James__|___2___|
|__James__|___2___|
and I need to get from first table output like :
|_name____|value_|
| James | 2 |
| | 2 |
| | 3 |
|_________|___6__|
| Jerry | 4 |
|_________|___5__|
Any ideas?
this also works. It stores the last record in #old_name and compare it
SELECT
IF(name = #old_name ,'',(#old_name := name)) AS name
,value
FROM mytable
,(SELECT #old_name:='')AS tmp
ORDER BY name,value;
|_name____|value_|
| James | 2 |
| James | 2 |
| James | 3 |
| James | 6 |
| Jerry | 4 |
| Jerry |___5__|
only like this (select * from $table_name order by name;),
cant like this:
|_name____|value_|
| James | 2 |
| | 2 |
| | 3 |
|_________|___6__|
| Jerry | 4 |
|_________|___5__|
SELECT CASE WHEN
value = (SELECT MIN(value) FROM table t WHERE t.name = name)
THEN name
ELSE ''
END AS name, value
FROM table
ORDER BY name ASC, value ASC
Bernd Buffen.. Your Select return something like that :/ :
|_name____|value_|
|_________| 2 |
|_________| 2 |
|_________| 3 |
|_________| 4 |
|__James__| 5 |
|__Jerry__|___6__|

Random order for group of rows in mysql

maybe it´s easy but i have no clue how to handle this correctly. I have the following table t1 with this data:
-----------------
| id | gr_id |
-----------------
| 1 | a |
| 2 | a |
| 3 | b |
| 4 | b |
| 5 | c |
| 6 | c |
| 7 | d |
| 8 | d |
-----------------
I would like to get randomly gr_ids like this:
-----------------
| id | gr_id |
-----------------
| 3 | b |
| 4 | b |
| 5 | c |
| 6 | c |
| 7 | d |
| 8 | d |
| 1 | a |
| 2 | a |
-----------------
Getting ordered gr_ids ascend and descend is pretty easy, but getting randomly grouped results, is pretty more complicated than i thought it is.
I do not get it, when i use GROUP BY or sth. similar i get for sure only one row each group. How can i randomly order groups, what is the trick here???
Thank you guys for bringing light into darkness ;)
E.g.:
SELECT x.*
FROM my_table x
JOIN
( SELECT DISTINCT gr_id
, RAND() rnd
FROM my_table
) y
ON y.gr_id = x.gr_id
ORDER
BY y.rnd
, x.id;
ORDER BY RAND() should work for you.

How to select full left table and where condition match records from right table?

Please some one tell me how to do this stuff. i have two tables , i need to select entire first table(pages) and from sencond table where user_id = 1
table 1: page
--------------------------------------
page_id | page_url | details |
--------------------------------------
1 | xxxx | wdredrr |
2 | yyyy | rdsacsa |
3 | zzzz | rscsacc |
4 | aaaa | xdsxsxs |
5 | bbbb | drxcraa |
--------------------------------------
table 2: control
-------------------------------------
control_id | page_id | user_id |
-------------------------------------
1 | 1 | 1 |
2 | 3 | 1 |
3 | 4 | 1 |
4 | 1 | 2 |
5 | 2 | 2 |
-------------------------------------
and this is what expecting output.
expecting output
--------------------------------------------------------------
page_id | page_url | details | control_id | user_id |
--------------------------------------------------------------
1 | xxxx | wdredrr | 1 | 1 |
2 | yyyy | rdsacsa | null | null |
3 | zzzz | rscsacc | 2 | 1 |
4 | aaaa | xdsxsxs | null | null |
5 | bbbb | drxcraa | 3 | 1 |
--------------------------------------------------------------
pages JOIN control ON page.page_id = control.page_id WHERE control.user_id = '1'
please someone help to solve this problem. i tried with LEFT JOIN and RIGHT JOIN but i get user_id = 1 matched rows only
Inner join will check if the related data is available with the join condition in the related tables and hence it will filter out unmatched data. You need to use left join and the conditions in the joining clause something as
select
p.*,
c.control_id,
c.user_id
from page p left join control c on c.page_id = p.page_id and c.user_id = 1
order by p.page_id;
Put the your where clause in the on clause and you should get the expected result:
select * from pages JOIN control ON page.page_id = control.page_id AND control.user_id = '1'
Try this
SELECT p.page_id, p.page_url, p.details, IF(c.user_id=1,c.control_id,NULL), IF(c.user_id=1,c.user_id,NULL) FROM page p LEFT JOIN control c ON(c.page_id=p.page_id)