I have following MySQL query result, I want only UserName first part before # it need 10000013 part only, I want to remove example.com part, it is possible by TRIM?
mysql> SELECT UserName,DAY(AcctStartTime), COUNT(ResponseCode='200') FROM table201412 WHERE UserName='10000013#example.com' GROUP BY DATE(AcctStartTime);
+---------------------------+--------------------+------------------------------+
| UserName | DAY(AcctStartTime) | COUNT(ResponseCode='200') |
+---------------------------+--------------------+------------------------------+
| 10000013#example.com | 1 | 3 |
| 10000013#example.com | 2 | 5 |
| 10000013#example.com | 3 | 3 |
+----------------------+--------------------+------------------------------+
10 rows in set (0.00 sec)
I need following result:
+---------------------------+--------------------+------------------------------+
| UserName | DAY(AcctStartTime) | COUNT(ResponseCode='200') |
+---------------------------+--------------------+------------------------------+
| 10000013 | 1 | 3 |
| 10000013 | 2 | 5 |
| 10000013 | 3 | 3 |
+---------------------------+--------------------+------------------------------+
10 rows in set (0.00 sec)
The easiest way is to use substring_index():
select substring_index(UserName, '#', 1) as EmailName
In your query, that would be:
SELECT substring_index(UserName, '#', 1) as EmailName, DAY(AcctStartTime), COUNT(ResponseCode='200')
FROM table201412
WHERE UserName='10000013#example.com'
GROUP BY DATE(AcctStartTime);
Related
I have a Mysql table like this:
+--------+--------------+
| idform | user_id |
+--------+--------------+
| 17 | 2 |
| 16 | 2 |
| 15 | 2 |
| 14 | 2 |
| 13 | 18 |
| 12 | 18 |
| 11 | 18 |
| 10 | 18 |
| 9 | 18 |
| 8 | 1 |
| 6 | 2 |
| 5 | 2 |
| 3 | 2 |
| 1 | 2 |
+--------+--------------+
14 rows in set (0.00 sec)
I need a query that gives me a result like this:
+----------------+--------------+
| idform | user_id |
+----------------+--------------+
| 17,16,15,14 | 2 |
| 13,12,11,10,9 | 18 |
| 8 | 1 |
| 6,5,3,1 | 2 |
+----------------+--------------+
4 rows in set (0.00 sec)
I tried to use GROUP_CONCAT() function of MYSQL but i couldn't make the result look like this. All i want to do is, MYSQL return the results in order but creates a new group for new user. Create a new group add ids with comma, then on a new user_id, create a new group.
I know i can make it by programmatic way with PHP but if i make it with PHP, i have some problems on pagination.
Any idea how to do this?
Thank you by now.
EDIT:
I also tried the query like this: SELECT GROUP_CONCAT(idform) FROM story GROUP_BY user_id. But it gives the result like this:
+---------------------+--------------+
| idform | user_id |
+---------------------+--------------+
| 8 | 1 |
| 1,3,5,6,14,15,16,17 | 2 |
| 9,10,11,12,13 | 18 |
+---------------------+--------------+
3 rows in set (0.00 sec)
You need to compare consecutive user-id's and after comparing assign each group a number. Later on, you can use group_concat over the data with that group_number.
I think below query should work for you.
SELECT GROUP_CONCAT(idform)
FROM (
SELECT
story.*
, #groupNumber := IF(#prev_userID != user_id, #groupNumber + 1, #groupNumber) AS gn
, #prev_userID := user_id
FROM story
, (SELECT #groupNumber := 0, #prev_userID := NULL) subquery
) sq
GROUP BY gn;
I have two tables testa and testb.
mysql> select * from testa;
+------+-------+
| id | dcac |
+------+-------+
| 1 | hello |
| 2 | world |
+------+-------+
2 rows in set (0.00 sec)
mysql> select * from testb;
+------+------+
| a_id | x |
+------+------+
| 1 | a |
| 1 | b |
| 1 | b |
| 1 | c |
| 2 | x |
+------+------+
5 rows in set (0.00 sec)
How do I add a column (x_list) to testa such that it's the comma-seperated list of xs from testb wherever testa.id is testb.a_id
So the output I'm expecting is somewhat like this -
+------+-------+--------+
| id | dcac | x-list |
+------+-------+--------+
| 1 | hello | a,b,b,c|
| 2 | world | x |
+------+-------+--------+
I tried using some complex join statements and I looked at this http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat (did not really understand much)
But I'm not able to proceed. What do I do?
Thanks.
Try:
SELECT testa.id, testa.dcac, GROUP_CONCAT(testb.x)
FROM testb
INNER JOIN testa ON testb.a_id = testa.id
GROUP BY testb.a_id
You can see result here: http://sqlfiddle.com/#!2/28887/2/0
I have two simple tables.
mysql> select * from filesshare;
+----+--------+--------+-------+
| id | userId | fileId | owner |
+----+--------+--------+-------+
| 1 | 2 | 1 | 1 |
| 2 | 3 | 1 | 1 |
| 3 | 4 | 2 | 1 |
| 4 | 5 | 2 | 1 |
+----+--------+--------+-------+
4 rows in set (0.00 sec)
mysql> select * from filesinfo;
+----+-----------------+-----------------------------------+
| id | name | url |
+----+-----------------+-----------------------------------+
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
+----+-----------------+-----------------------------------+
2 rows in set (0.01 sec)
i will have now two values as input. 1) userId 2) owner
now assume userId=3 and owner=1. Now i want out put to match all the rows with given values in filesshare table in this case its second row that is " 2 | 3 | 1 | 1 " now i want that column three value which is 1 now. Then with that value i want to retrieve the data from filesinfo table. in this case the first row of filesinfo table will b the output. like this:
+----+-----------------+-----------------------------------+
| id | name | url |
+----+-----------------+-----------------------------------+
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
+----+-----------------+-----------------------------------+
is that possible using joins using hibernate, a simple example can be a kickstart for me.
i tried this
mysql> Select fileid,name,url from filesshare fs,filesinfo fi where fs.fileid=fi
.id;
+--------+-----------------+-----------------------------------+
| fileid | name | url |
+--------+-----------------+-----------------------------------+
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
+--------+-----------------+-----------------------------------+
Thanks and regards
in hibernate you can deal with it two ways:
use plain sql
example:
SQLQuery query = session.createSQLQuery("select fi.* from fileshare fs, filesinfo fi where fs.fileId =fi.id and fs. userId = :uid and fs.owner = :oid");
query.setInteger("uid", userid);
query.setInteger("oid", ownerid);
/*maybe deal with transformation */
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);)
return (List<Map<String,Object>>)query.list();
here is the link with info on transformation details:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-associations
use hql assuming you have entities mapped to
example:
Criteria crit = session.createCriteria(Filesinfo.class)
crit.setFetchMode("fs.fileshare", FetchMode.JOIN);
crit.add( Restrictions.eq("fs.userId", userid) Restrictions.eq("fs.owner", ownerid) );
return crit.list();
all info on this method is here: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html
and here is the details of teh ORM mapping via xml: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/xml.html
Select id,name,url from fileshare fs,filesinfo fi where fs.id=fs.id;
k guys this one one after scratching head, of course help from google also.
mysql> select a.id, a.name, a.url from filesinfo a inner join filesshare b on
a.id=b.fileid where b.userid=5 and b.owner=1;
+----+-----------------+-----------------------------------+
| id | name | url |
+----+-----------------+-----------------------------------+
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 4 | loader3.gif | C:\shareapp\admin\loader3.gif |
+----+-----------------+-----------------------------------+
2 rows in set (0.02 sec)
I have 4 tables:
ARTICOLE
BAR
BUCATARIE
MAGAZIE
mysql> select * from ARTICOLE;
| OID | ART |
| 1 | TEST |
| 2 | TESTQ |
| 3 | MYART |
| 4 | MYARTBUC |
4 rows in set (0.00 sec)
mysql> select * from BAR;
| OID | ART | CANT |
| 1 | TEST | 3.00000 |
| 2 | TESTQ | 1.00000 |
| 3 | MYART | 20.00000 |
3 rows in set (0.00 sec)
mysql> select * from BUCATARIE;
| OID | ART | CANT |
| 1 | TEST | 5.00000 |
| 2 | MYARTBUC | 10.00000 |
2 rows in set (0.00 sec)
mysql> select * from MAGAZIE;
Empty set (0.00 sec)
the below query
mysql> select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT) from ARTICOLE a,BUCATARIE buc,BAR bar,MAGAZIE mag where a.ART=bar.ART and a.ART=bar.ART and a.ART=mag.ART group by a.ART;
return:
Empty set (0.00 sec)
how must be query to return:
| ART | sum(bar.CANT) | sum(buc.CANT) | sum(mag.CANT) |
TEST | 3.00000 | 5.00000 | NULL |
TESTQ | 1.00000 | NULL | NULL |
MYART | 20.00000 | NULL | NULL|
MYARTBUC | NULL | 10.00000 | NULL |
????
Any help appreciated.
You need to make use of LEFT JOIN to include results from other tables without filtering out records that don't match:
select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT)
from ARTICOLE a
left join BUCATARIE buc on buc.ART = a.ART
left join BAR bar on bar.ART = a.ART
left join MAGAZIE mag on mag.ART = a.ART
group by a.ART;
Sample results:
ART SUM(BAR.CANT) SUM(BUC.CANT) SUM(MAG.CANT)
MYART 20
MYARTBUC 10
TEST 3 5
TESTQ 1
Demo: http://www.sqlfiddle.com/#!2/6efb2/2
How can I update this to select all duplicates?
SELECT address FROM list
GROUP BY address HAVING count(id) > 1
Currently, I think it just returs the addresses which are duplciated. I want all duplicates.
Select * from list
where address in (
select address from list group by address
having count(*) > 1);
Look at this sample query I ran:
mysql> select * from flights;
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 6 | 1 |
| 2 | 4 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
10 rows in set (0.00 sec)
mysql> select * from flights where source in
(select source from flights group by source having count(*) > 1);
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 5 | 6 |
| 6 | 1 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
8 rows in set (0.00 sec)
If I'm correct, you're looking for the actual rows that contain duplicates -- so that if you have three rows with the same address, you return all three rows.
Here's how to do it:
SELECT * FROM list
WHERE address in (
SELECT address FROM list GROUP BY address HAVING count(id) > 1
);
This should generally work unless your address is a 'text' field or if your address table has more than a few thousand duplicates.
Are you looking for this?
SELECT * FROM list
WHERE id IN (
SELECT id FROM list
GROUP BY address HAVING count(id) > 1
);