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
Related
I have a table:---
id
name
dept
1
Alice
CS
2
Bob
Elect
3
David
Mech.
and a query result:-
id
count
1
100
2
22
3
50
Then I want to add the count column from the query to my original table, something like:-
id
name
dept
count
1
Alice
CSE
100
2
Bob
Elect
22
3
David
Mech.
50
The only I figured out to do, is by storing the query result into a new table and then using UPDATE...SET...WHERE. Is there any way to do it without creating a new table?
First you need to create the count column in tablename using
ALTER TABLE `tablename` ADD COLUMN `nr_count` INT;
Then use:
update tablename t
inner join ( SELECT id,
count(*) as nr_count
FROM tablename
GROUP BY id
) as t1
set t.nr_count=t1.nr_count ;
I'm stuck for hours on an issue that might be pretty simple to solve but I'm just so lost...
I got 3 tables :
user
id name
----------
1 jack
2 john
...
car
id name
----------
1 ford
2 fiat
3 alfa
4 lada
...
user_car
id_user id_car
-----------------
1 2
1 4
2 1
2 2
2 3
For example, i want to get all users with cars which have id 1 AND 2 in the user_car table so I should get the id_user 2 only and I can't find the proper way to do it.
try this untested query:
select * from user join user_car car1 on id =car1.user_id
join user_car car2 on id =car2.user_id where car1.id_car=1 and car2.id_car=2
I would use UNION for this matter:
SELECT id as id_user from user where id in(1, 2)
UNION
SELECT id as id_car from car where id in (1, 2)
You can use COUNT to do this:-
SELECT user.name
FROM user
INNER JOIN user_car ON user.id = user_car.id_user
INNER JOIN car ON user_car.id_car = car.id
WHERE car.id IN (1,2)
GROUP BY user.name
HAVING COUNT(DISTINCT car.id) = 2
Note that this could be simplified to remove the need for the car table when you are just using the id of the car.
May be you want this
SELECT *
FROM USER
WHERE id IN
(SELECT id_user
FROM user_car
WHERE id_car=1 OR id_car=2);
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.
I have three tables:
TABLE 1 contracts
-------------------
id | contract_name
--------------------
1 test name
2 test name 2
2 test name 3
4 test name 4
TABLE 2 rooms
-------------------
id | room_name
--------------------
1 test name
2 test name 2
2 test name 3
4 test name 4
TABLE 3 promos
----------------------------------
id | contracts_id | rooms_id
----------------------------------
1 1,3 1,3,4
1 2 1,2,3
No I am trying to do an inner join to get the names of the contract and the rooms according to the ids in the array saved in database. I know this is not ideal at all, but I can not change the database set up. So here is what I would like to do with my query, but obviously it is impossible. Does anyone have any idea on how I can accomplish this?
mysql_query("SELECT pb.*, c.contract_name, r.room_name FROM promo_blackouts AS pb
INNER JOIN contracts as c ON c.contract_id IS IN pb.contracts_id
INNER JOIN rooms as r ON r.room_id IS IN pb.rooms_id
WHERE pb.promo_id = '$promo_id'") or die(mysql_error());
Are you looking for something like this?:
SELECT DISTINCT
contract_name,
room_name
FROM
promos
INNER JOIN
contracts ON FIND_IN_SET(contracts.id, contract_id) != 0
INNER JOIN
rooms ON FIND_IN_SET(rooms.id, room_id) != 0
WHERE
promos.id = 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).