Here is my DB Structure:
CREATE TABLE IF NOT EXISTS `UserItems` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` int(10) unsigned NOT NULL,
`item_id` int(10) unsigned NOT NULL,
`qty` int(11) NOT NULL default '0'
) ;
CREATE TABLE IF NOT EXISTS `UserEquippedItems` (
`user_id` int(10) unsigned NOT NULL,
`user_item_id` bigint(20) unsigned NOT NULL
);
CREATE TABLE IF NOT EXISTS `UserFriendEquippedItems` (
`user_friend_id` int(10) unsigned NOT NULL,
`user_item_id` bigint(20) unsigned NOT NULL
);
UserItems keeps all the item inventory with quantity.
let say if I have 5 item (item id 123456). then the entry will be
(null, $userid, 123456, 5).
the qty is the quantity of all the entity with the same item_id.
However, some Users may equip the item. Some do not. If they equip it, it will be an entry in UserEquippedItems table.
Also, users' friends can equip the user's item too.
Sample Data:
UserItems:
id, user_id, item_id, qty
( 1, 4567, 123123123, 5)
( 2, 4567, 100010001, 2)
( 3, 4567, 100010099, 1)
UserEquippedItems: (user_item_id is UserItems.id)
user_id, user_item_id
( 4567, 1)
( 4567, 2)
UserFriendEquippedItems
(user_item_id is UserItems.id)
user_friend_id, user_item_id
( 4100, 1)
( 4100, 3)
So, how can I find out the quantity of items that are equipped?
and how can I find out the quantity of items that are NOT equipped ?
Side Story: Before, we have each individual UserItems as a single entry in the DB. e.g. for item_id = 123123123, if I have 5 of them, i have 5 entries in the DB. But then, our DB grows like crazy to 4 million UserItems records. that's why instead of having 5 entries, we only have one entry in the UserItems table, with the qty field for keep track how many in total. I don't know if it is the right approach, but I hope it can cut down 75% of the DB size.
Also, it was the query to get the unequipped items:
SELECT Items.id, count(UserItems.id) as numCount
FROM UserItems INNER JOIN
Items ON UserItems.active=1 AND
UserItems.item_id=Items.id AND
Items.active=1 AND
UserItems.user_id=$userId
WHERE NOT EXISTS (
SELECT UserEquippedItems.user_item_id
FROM UserEquippedItems
WHERE UserEquippedItems.user_item_id= UserItems.id
)
AND NOT EXISTS (
SELECT UserFriendsEquippedItems.user_item_id
FROM UserFriendsEquippedItems
WHERE UserFriendsEquippedItems.user_item_id= UserItems.id)
GROUP BY Items.id
Of course, this query doesn't work with the new schema. :)
This should work:
SELECT id, user_id, item_id, qty,
(SELECT COUNT(*) FROM UserEquippedItems uei
WHERE uei.user_item_id=ui.id) as qty_equipped,
(SELECT COUNT(*) FROM UserFriendEquippedItems ufei
WHERE ufei.user_item_id=ui.id) as qty_friend_equipped
FROM UserItems ui
Then to get the unequipped, on your client you can subtract the qty_equipped and qty_friend_equipped from qty.
A single query that just returns unequipped:
SELECT id, user_id, item_id, qty,
qty-
(SELECT COUNT(*) FROM UserEquippedItems uei
WHERE uei.user_item_id=ui.id)-
(SELECT COUNT(*) FROM UserFriendEquippedItems ufei
WHERE ufei.user_item_id=ui.id)
as qty_unequipped
FROM UserItems ui
You could combine those 2 queries above into one big query, but I image that would hurt performance, since it will run the sub-queries twice.
You can add a WHERE clause on the end of both of these to return results for a specific user / item.
To count the total number of items equipped:
SELECT count(*) FROM UserEquippedItems e JOIN UserItems i ON (i.id = e.user_item_id)
To find the total number of items of id $item_id equipped
SELECT count(*) FROM UserEquippedItems e WHERE e.user_item_id = $item_id
And to find the total number of items user $user_id has equipped (my first response, though probably not what you're after)
SELECT count(*) FROM UserEquippedItems WHERE user_id = $user_id
Related
I am working on a php project for managing employees.
i have two tables employee and department with the department number being a relation between the two. department has an attribute that contains a count of employees based on the deparment number.
So where i'm stuck is that i want the employee count to be automatically inserted, so what do you think i should do
Thanks in advance
If you don't want to use (or have access to) a trigger, but want to abstract the logic, you could use a view (docs).
So assuming the below data, and the idea of having the entire department table and the employee count dynamically calculated, you could have the following query:
CREATE TABLE
`employees`
(
`employeeID` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(50),
`department_number` INT
);
CREATE TABLE
`departments`
(
`department_number` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(50)
);
INSERT INTO
`departments`
(`department_number`, `name`)
VALUES
(1, 'Tech Department'),
(2, 'Admin Department');
INSERT INTO
`employees`
(`name`, `department_number`)
VALUES
('John Doe', 1),
('Jane Doe', 1),
('Jimmy Doe', 2);
Then the query:
SELECT
DepartmentTbl.*,
DepartmentEmployeeTbl.employee_count
FROM
`departments` AS DepartmentTbl
LEFT JOIN
(
SELECT
`department_number`,
COUNT(`employeeID`) AS `employee_count`
FROM
`employees`
GROUP BY
`department_number`
) AS DepartmentEmployeeTbl
ON DepartmentTbl.department_number = DepartmentEmployeeTbl.department_number
Gives the result:
department_number | name | employee_count
-------------------------------------------------------
1 | Tech Department | 2
2 | Admin Department | 1
SQLFiddle - http://sqlfiddle.com/#!9/3e0b54/1
So to create this view, you could use:
CREATE OR REPLACE VIEW `departments_employee_count` AS
SELECT
DepartmentTbl.*,
DepartmentEmployeeTbl.employee_count
FROM
`departments` AS DepartmentTbl
LEFT JOIN
(
SELECT
`department_number`,
COUNT(`employeeID`) AS `employee_count`
FROM
`employees`
GROUP BY
`department_number`
) AS DepartmentEmployeeTbl
ON DepartmentTbl.department_number = DepartmentEmployeeTbl.department_number
Then you could call the view as:
SELECT
*
FROM
`departments_employee_count`
Instead of storing and updating the value each time something is changed you should just calculate it when needed.
You can use count to do it.
Example
SELECT department_number, count(*) FROM employee GROUP BY department_number
Let's jump right into my problem and imagine that we have this simple table
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `test` (`id`, `score`)
VALUES
(1, 10),
(2, 15),
(3, 6),
(4, 17);
What I want to do is the following. I want to sort by score and then, get the rank of the field with the ID 3
Since the entry with the ID 3 has the lowest score it would be rank 4.
I have this query, to get the ranks sorted by score
SELECT #rownum:=#rownum+1 rank, id, score from test t, (SELECT #rownum:=0) r ORDER BY score DESC;
The result is the following
rank || id || score
1 4 17
2 2 15
3 1 10
4 3 6
However, what I want to achieve is to get the rank instantly. Without having the ranks and results of the other values. I can't just add a WHERE CLAUSE with the condition that the ID should be 3, because If I do so the rank is always 1 because the query only matches one entry in the database.
How would I get the rank directly without iterating somehow over the result?
Why not just count how many are better than the id you want the rank of?
For example for id 3:
SELECT count(*)+1 FROM test where score > (SELECT score from test WHERE id = 3))
Just change the id at the end of the subquery to get the rank of another id
Add a index to score column and use below query-
select count(*) from test a join test b on a.score > b.score where a.id =3
I'm working on a PHP project with MYSQL database. I have a table of groups of students. Each group has an examiner. What i want to do is that i want to set two examiners for each group randomly. How to do it?
MySQL Code:
create table groups (
groupID int(10) not null,
nbStudents int not null,
avgGPA DOUBLE NOT NULL,
projectName varchar(50) not null,
advisorID int,
examiner1ID int,
examiner2ID int,
adminID int not null,
primary key (groupID)
);
create table faculty (
name varchar(30) not null,
facultyID int(10) not null,
email varchar(30) not null,
mobile int(15) not null,
primary key (facultyID)
);
examiner1ID and examiner2ID are foreign keys from the table faculty.
Here is a very convoluted way to do it. It uses 2 subqueries to pick faculty members, and insert .. on duplicate key to update the examiners IDs.
insert into groups
(groupID, examiner1ID, examiner2ID)
select groupID,
#x:=(select facultyID from faculty order by rand() limit 1),
(select facultyID from faculty where facultyID <> #x order by rand() limit 1)
from groups
on duplicate key update examiner1ID=values(examiner1ID), examiner2ID=values(examiner2ID);
#x is a user-defined-variable. In this case, it is used to store the first random faculty member. <> #x makes sure we don't pick the same faculty member in both slots.
Since groupID is a unique key, when we try to insert a row with an existing unique key, it will update the existing row instead of inserting it. That's what on duplicate key update clause is used for.
set different examiners for each group:
insert into groups
(groupID, examier1ID, examier2ID)
select a.groupID, max(if(b.id%2, b.facultyID, 0)), max(if(b.id%2, 0, b.facultyID))
from (
select #row:=#row+1 id, groupID
from groups a
join (select #row:=0) b) a
join (
select #row:=#row+1 id, facultyID
from (
select facultyID
from faculty a
order by rand()) a
join (select #row:=0) b) b on a.id = ceil(b.id/2)
group by a.groupID
on duplicate key update examiner1ID=values(examiner1ID), examiner2ID=values(examiner2ID);
i have an income and expense table. i want to select them both in a single query and get the difference between the income.amount field and expense.amount field grouping by month. so that the query will return a result whereby the total of expenses amount from the total of income amount basing the calculation on month. i used used two methods but non worked. find the below:
SELECT *, count(*), sum(`transaction`.amount) as tiamount, sum(expenditure.amount) as teamount, monthname(`transaction`.date) as mni, monthname(expenditure.date) as mne
FROM `transaction`, expenditure
WHERE month(expenditure.`date`)=month(`transaction`.`date`) and month(`transaction`.`date`)=month(expenditure.`date`)
GROUP BY monthname(`transaction`.date) ORDER BY `transaction`.date Desc
the other is :
SELECT count(*), `transaction`.date, sum(`transaction`.amount) as tiamount, sum(`transaction`.amount - expenditure.amount) as diff, monthname(`transaction`.date) as mni
FROM `transaction` left join expenditure on monthname(`transaction`.date) = monthname(expenditure.date)
UNION ALL
SELECT count(*), expenditure.date, sum(expenditure.amount) as teamount, sum(`transaction`.amount - expenditure.amount) as diff, monthname(expenditure.date) as mne
FROM expenditure left join `transaction` on monthname(`transaction`.date) = monthname(expenditure.date)
any help would be appreciated. Thanks.
ok thanks all. i solved the issue. each time the page is loaded, it checks to see if the table balance2 already exist, if it does it is dropped and recreated on the fly
mysql_query($query_truncate = "drop table IF EXISTS balance2");
$create= "CREATE TABLE balance2 (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`count` VARCHAR( 50 ) NOT NULL DEFAULT '',
`month` VARCHAR( 50 ) NOT NULL DEFAULT '',
`amount` VARCHAR( 50 ) NOT NULL DEFAULT '',
`amount2` VARCHAR( 20 ) NOT NULL ,
`type` VARCHAR( 20 ) NOT NULL ,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY ( `id` )
) ENGINE = INNODB DEFAULT CHARSET = latin1";
mysql_query($create);
Then in used the INSERT INTO...SELECT...UNION SELECT....
to select from the expenses and income table to insert into the newly created balance table.
query_merge = "INSERT INTO balance2 (count, date, amount, amount2, month, type)
(SELECT count(*), t.date, sum(t.amount),0 ,monthname(t.date), 'income'
FROM `transaction` as t group by month(t.date))
UNION
(SELECT count(*), e.date, 0,sum(e.amount) as teamount, monthname(e.date) as mne, 'expense'
FROM expenditure as e group by month(e.date))";
this worked perfectly as it gave me all the results i wanted.
Thought it might help someone else. see ya
Say I have three tables in my database:
CREATE TABLE `users` (
`user_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR(16) NOT NULL
);
CREATE TABLE `users_meta` (
`meta_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`user_id` INT NOT NULL ,
`key` VARCHAR(200) NOT NULL ,
`value` TEXT NOT NULL
);
CREATE TABLE `posts` (
`post_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`user_id` INT NOT NULL ,
`content` TEXT NOT NULL
);
The table users_meta is just a key-value store of information about users, such that we can add any piece of information we want.
Say I added a key => value pair to the users_meta table for each user where the key was "age", and the value was a number representing their age.
Given this set of circumstances, what's the best way to select the first 10 posts ordered by user age?
I like putting the condition of the join in the join itself to be clear that I want a limited join:
SELECT p.post_id, p.content
FROM users u
INNER JOIN users_meta um
ON (u.user_id = um.user_id) AND um.key = 'age'
INNER JOIN posts p
ON (p.user_id = u.user_id)
ORDER BY um.value
limit 10
If you order by user age only, you will select 10 posts of the same user (the youngest one).
I would suggest to denormalize and store age in users table directly.
Agree with #KOHb, but if that's exactly what you want, here is the query:
SELECT TOP 10 p.id, p.content
FROM users u JOIN users_meta um ON (u.user_id = um.user_id)
JOIN posts p ON (p.user_id = u.user_id)
WHERE um.key = 'age'
ORDER BY um.value