Maybe this question is duplicate. here situation is different that's why I put the question..
from table m_groups and m_group_admin
select m_groups.GROUP_CREATOR_ID as GROUP_ADMIN
from m_groups where m_groups.GROUP_ID='6'
union
select m_group_admin.GROUP_ADMIN from m_group_admin
where m_group_admin .GROUP_ID='6';
above query returns a column like
| GROUP_ADMIN |
---------------
4
8
2
I need user id, first name, last name (m_user_info.USER_ID, m_user_info.FIRST_NAME, m_user_info.LAST_NAME) from table m_user_info for the above outputs
I need output like this
| USER_ID | |FIRST_NAME| |LAST_NAME |
----------- ------------- ------------
4 ferdous wahid
8 sumon sumon
2 rahul paul
Try this:
SELECT USER_ID,FIRST_NAME,LAST_NAME
FROM m_user_info
WHERE USER_ID IN
(select m_groups.GROUP_CREATOR_ID as GROUP_ADMIN
from m_groups where m_groups.GROUP_ID='6'
union
select m_group_admin.GROUP_ADMIN from m_group_admin
where m_group_admin .GROUP_ID='6';)
Explanation:
It will select USER_ID,FIRST_NAME and LAST_NAME from users table with the ids you have got.
Related
I have a table in which I want to compare an ID column with another column in the same table.
This is my table
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
4 Alex 5
For example I want to use
select * from mytable where ID = BossID
and I expected to get that Peter is the Boss of John and Alex is the Boss of Max and Peter, but when I use it this way, I dont get any info on the query...
Any idea to get it?
The expected query result are:
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
seems like you want the list of employees who their boss exists in the list as well:
select *
from mytable
where bossid in (select id from mytable);
to get the boss name :
select t.ID, t.Name, b.Name as BossName
from mytable t
join mytbale b
on t.bossid = b.id
you can use left join to return all the employees in the list
I have 3 MySQL queries I'd like to combine as 1
SELECT pic FROM active,
SELECT pic FROM deleted,
SELECT alt_pic FROM active where alt_pic!=''
I've managed to get the first 2 as one
SELECT pic FROM active UNION SELECT pic FROM deleted
I think I've partially gotten through combining all 3 except I don't know where exactly to insert the 3rd query
SELECT COUNT(*) FROM (SELECT pic FROM active UNION SELECT pic FROM deleted)t
I am just studying for fun. If I am somehow breaking convention or introduce some security risks, please don't get mad :)
Edit 1: Newbie doesn't know, thanks Mureinik and Strawberry for pointing it out :)
alt_pic is just a very optional field, my table active has about 300+ rows but only 8 alt_pic fields filled
active
ID | name | pic | alt_pic
1 | Peter | pic5.jpg | alt1.jpg
2 | Mark | pic4.jpg | NULL
3 | John | pic3.jpg | alt2.jpg
deleted
ID | pic
1 | pic2.jpg
2 | pic1.jpg
The result I'd like to have is
pic_count | alt_pic_count
5 | 2
I'd perform an aggregate query on each table, and then, since they both return just one row, cross join them. Note you don't need a third query with a condition on alt_pic - since count ignores nulls, you could apply it directly to that column.
SELECT pcount + dcount AS pic_count, acount AS alt_pic_count
FROM (SELECT COUNT(pic) AS pcount, COUNT(alt_pic) AS acount
FROM active) a
CROSS JOIN (SELECT COUNT(*) AS dcount
FROM deleted) d
I am trying to get distinct result of following table
id | name | created_on
1 | xyz | 2015-07-04 09:45:14
1 | xyz | 2015-07-04 10:40:59
2 | abc | 2015-07-05 10:40:59
I want distinct id with latest created_on means following result
1 | xyz | 2015-07-04 10:40:59
2 | abc | 2015-07-05 10:40:59
How to get above result by sql query?
Try this:
Select id, name, max(created_on) as created_on from table group by id
Try:
select id,max(name), max(created_on) from table_name group by id
Additional Note:
As it appears, your table is not normalized. That is, you store the name along with id in this table. So you may have these two rows simultaneously:
id | name | created_on
1 | a | 12-12-12
1 | b | 11-11-11
If that state is not logically possible in your model, you should redesign your database by splitting this table into two separate tables; one for holding id-name relationship, and another to hold id-created_on relationship:
table_1 (id,name)
table_2 (id,created_on)
Now, to get last created_on for each id:
select id,max(created_on) from table_2
And if you want to hold name in the query:
select t1.id, t1.name, t2.created_on from table_1 as t1 inner join
(select id, max(created_on) as created_on from table_2) as t2
on t1.id=t2.id
Assuming that id/name is always a pair:
select id, name, max(created_on)
from table
group by id, name;
It is safer to include both in the group by. I also find it misleading to name a column id when it is not unique for the table.
You can use the keyword DISTINCT
like
SELECT DISTINCT
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've got 2 tables. The first table is full of entries. The second table defines what categories that entry belongs to:
Table 1:
entry_id | title
1 | Entry1
2 | Entry2
3 | Entry3
Table 2
entry_id | cat_id
1 | 233
1 | 234
1 | 678
2 | 235
2 | 453
2 | 21
3 | 234
3 | 233
I'm trying to select an entry with a single query of all posts belonging to multiple categories. For example, I want to return the entries belonging to category ids, 233 and 234. I believe this needs a subquery although I'm not quite sure. Any help anybody? :)
Learn about SQL joins.
SELECT * FROM tbl1 JOIN tbl2 USING (entry_id) WHERE cat_id IN (233,234);
See it on sqlfiddle.
UPDATE
To select all entries in both categories, you could group the results of the join and only select those groups that have contain both categories:
SELECT tbl1.*
FROM tbl1 JOIN tbl2 USING (entry_id)
WHERE cat_id IN (233,234)
GROUP BY entry_id
HAVING COUNT(DISTINCT cat_id) = 2
See it on sqlfiddle.
The COUNT(DISTINCT cat_id) can obviously be replaced with the (much less expensive) COUNT(*) if (entry_id, cat_id) is known to be unique in tbl2.
Try this:
select * from entity e
where exists (select * from category c where c.entry_id=e.entry_id AND c.cat_id=233)
and exists (select * from category c where c.entry_id=e.entry_id AND c.cat_id=234)
This returns rows that belong to both 233 and 234 (this is how I read your question, anyway; I may have misunderstood the "belonging to multiple categories" part).