I have two tables:
Users Table:
ID | Name | UserGroupID |
1 | John | 2
2 | Sam | 11
3 | Kiddo | 2
4 | Sony | 3
5 | Jabeen | 1
UsersMachine Table:
ID | MachineID | EmpID
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 2 | 2
5 | 2 | 4
6 | 3 | 5
i m looking at single sql to repeat all Users by filtering
FROM UserMachine WHERE MachineID = 2 ALSO FROM Users WHERE UsersGroupID IN (2,11) with these conditions
I m looking at following reasults:
MachineID | UsersName | UsersGroupID
2 | John | 2
2 | Sam | 11
Check out joins. They allow us to associate data from two different tables in a single query:
SELECT um.MachineId, users.Name, users.UsersGroupID FROM UsersMachine um
INNER JOIN Users users ON users.id = um.EmpID
WHERE um.MachineID = 2 AND users.UsersGroupID IN (2,11)
This is probably the best post (visually) I have seen that help me with joins:
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
Related
I have three tables in a database and want to sum and compare in one SELECT statement.
The logic is that if one article runs low in stock it should be selected. An article runs low if the summarized quantity of all rows with the same artID in the table Articles in stock (an article can have several rows in the table and therefore needs to be summarized) is lower or equal to the column warning in the row with the corresponding ID. This is also warehouse specific, meaning that an article can be fully stocked in one warehouse, and at the same time running low in another. Which is why it is needed to summarize grouped on a specific warehouse.
Information about which warehouse is running low, and such as artNr for the article is also needed, which can be found in table Articles
Articles
+----+-------+---------+
| ID | artNr | warning |
+----+-------+---------+
| 1 | LA08 | 5 |
| 2 | LA09 | 10 |
| 3 | LA58 | 0 |
+----+-------+---------+
warehouse
+----+-------+
| ID | artID |
+----+-------+
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
+----+-------+
Articles in stock
+----+-------+-------------+----------+
| ID | artID | warehouseID | quantity |
+----+-------+-------------+----------+
| 1 | 1 | 3 | 10 |
| 2 | 1 | 5 | 15 |
| 3 | 2 | 5 | 45 |
| 4 | 1 | 3 | 20 |
| 5 | 3 | 5 | 4 |
+----+-------+-------------+----------+
select a.id, s.warehouseID, sum(s.quantity)
from articles a
join articles_in_stock s on a.id = s.artID
group by a.id, s.warehouseID
having sum(s.quantity) <= a.warning
I am new to SQL and trying to fetch data using join. Don't know what I am doing wrong. I have two tables.
Table record
recordID|activityName|userID
1 | sale | 5
2 | call | 5
3 | contact | 5
Table activity
activityID|activityData|userID
1 | 50 | 5
2 | 70 | 5
3 | xyz | 5
Result should be
recordID|activityName|activityData|userID
1 | sale | 50 | 5
2 | call | 70 | 5
3 | contact | xyz | 5
But it returns
recordID|activityName|activityData|userID
1 | sale | 50 | 5
2 | call | 70 | 5
3 | contact | xyz | 5
1 | sale | 50 | 5
2 | call | 70 | 5
3 | contact | xyz | 5
1 | sale | 50 | 5
2 | call | 70 | 5
3 | contact | xyz | 5
SELECT * FROM tblrecord tr, tblactivity ta WHERE tr.userID = ta.userID AND tr.userID = '5'
Why is it repeating the result.
PS. userID is the only common field, the activityID and recordID can be different.
First, follow a simple rule: Never use commas in the FROM clause. Always use explicit JOIN syntax.
Second, you seem to want a join on recordId and activityId:
SELECT *
FROM tblrecord tr JOIN
tblactivity ta
ON tr.userID = ta.userID AND
tr.recordId = ta.activityId
WHERE tr.userID = 5;
Also, don't use single quotes if the value is really a number.
Add
GROUP BY userID, activityID
to your query.
I want add one filed to result of queries in MySQL.
this tables is my database tables :
tbl_user
id_user | username | password
-----------------------------
1 | Pooria | 123456
2 | mary | 123456
3 | july | 123456
4 | Ali | 123456
5 | shahla | 123456
tbl_category
id_category | name
--------------------
1 | BMW
2 | Toyota
3 | Benz
tbl_content
id_content | message | ContentLike
--------------------------------------
1 | Best Car | 2
2 | Best Car | 3
3 | Best Car | 4
4 | Best Car | 1
5 | Best Car | 1
tbl_user_category_content
id | id_user | id_category | id_content
----------------------------------------
1 | 2 | 2 | 4
1 | 3 | 3 | 3
1 | 4 | 3 | 5
1 | 5 | 1 | 2
tbl_user_like
id_user_like | id_user | id_content
-------------------------------------
1 | 2 | 5
1 | 2 | 3
1 | 5 | 1
1 | 4 | 2
1 | 4 | 2
My Query in Mysql :
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
and Result
id_content | message | ContentLike | username
----------------------------------------------
1 | Best Car | 2 | Pooria
2 | Best Car | 4 | mary
3 | Best Car | 3 | july
4 | Best Car | 5 | Ali
5 | Best Car | 4 | shahla
I expect Result :
I want insert a id_user and view Result:
example : insert id_user : 2 (mary)
id_content | message | ContentLike | username | Like_User
-------------------------------------------------------------------
1 | hello world1 | 4 | Pooria |
2 | hello world2 | 2 | mary |
3 | hello world3 | 2 | july | Yes
4 | hello world4 | 2 | Ali |
5 | hello world5 | 2 | shahla | Yes
add result Like_User
important:I want all entries in the database with the person who likes to show a distinct field. thanks you for help me <3
Add this in the select columns :
case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
So it would be something like this
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username, case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
I have not test it. Try it out and maybe you should change the column names in the case
Try This Query
SELECT distinct tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username,tbl_user_category_content.category
FROM tbl_user_category_content
INNER JOIN tbl_user,tbl_cat,tbl_post ON
tbl_user_category_content.id_user = tbl_user.id_user
tbl_user_category_content.id_category = tbl_cat.id_category
tbl_user_category_content.id_content = tbl_content.id_content
WHERE tbl_user.id_user='$user_id'
I have three tables and they are the following
User Table
+---------+-----------+--------+
| user_id | user_name | branch |
+---------+-----------+--------+
| 1 | John | 1 |
| 2 | Jim | 2 |
| 3 | Jern | 3 |
| 4 | Jack | 1 |
| 5 | Jery | 2 |
| 6 | Tom | 3 |
| 7 | Sona | 1 |
| 8 | Tina | 3 |
+---------+-----------+--------+
Branch Table
+-----------+----------------+
| branch_id | branch_name |
+-----------+----------------+
| 1 | IT |
| 2 | SALES |
| 3 | Administration |
+-----------+----------------+
Enquiry Table
+------------+---------------+---------+
| enquiry_id | enquiry_name | user_id |
+------------+---------------+---------+
| 1 | enqury_test1 | 1 |
| 2 | enqury_test2 | 2 |
| 3 | enqury_test3 | 1 |
| 4 | enqury_test4 | 3 |
| 5 | enqury_test5 | 2 |
| 6 | enqury_test6 | 5 |
| 7 | enqury_test7 | 1 |
| 8 | enqury_test8 | 2 |
| 9 | enqury_test9 | 4 |
| 10 | enqury_test10 | 6 |
| 11 | enqury_test11 | 2 |
| 12 | enqury_test12 | 7 |
+------------+---------------+---------+
From the above tables its clear that, each branch contains a number of users.
These users post multiple enquiries.
I need to get the total number of enquiries in each branch as
branch id => number of enquiries
I have tried various queries. But i couldn't get the result. Any one can help?
I am using MySQL and i need a single query to do this.
Thanks in advance
You need count and group by
select
b.branch_id,
count(e.user_id) as `total_enq`
from Branch b
left join User u on u.branch = b.branch_id
left join Enquiry e on e.user_id = u.user_id
group by b.branch_id
The query you have to perform to get you desired result is like this :-
$query = "SELECT u.branch, COUNT(u.user_id) AS `total_enquires`
FROM enquiry e INNER JOIN user u ON e.user_id = u.user_id
GROUP BY u.branch"
This will help you,and i think you don't need to join branch table as user table already contain branch_id.
This is the query
SELECT `branch`,`branch_name`,count(`user`.`user_id`),count(`enquiry_id`) FROM `user` inner join `branch` on `user`.`branch`=`branch`.`branch_id` inner join `enquiry` on `user`.`user_id`=`enquiry`.`user_id` group by `branch`
try it here
http://sqlfiddle.com/#!9/cf3eb/1
SELECT
bt.branch_id
,COUNT(enquiry_id) AS total_enquiry
FROM
enquiry_table et
INNER JOIN user_table ut on ut.user_id = et.user_id
INNER JOIN branch_table bt ON bt.branch_id = ut.branch
WHERE
1=1
GROUP BY
bt.branch_id
you can try this
I have two tables:
Students Student_Grades
V------------------------V
+----+------+ +----+------------+---------+-------+
| id | name | | id | student_id | subject | grade |
+----+------+ +----+------------+---------+-------+
| 0 | Dave | | 0 | 0 | Math | 100 |
+----+------+ +----+------------+---------+-------+
| 1 | John | | 1 | 0 | Chem | 90 |
+----+------+ +----+------------+---------+-------+
| 2 | Kate | | 2 | 0 | CompSCI | 95 |
+----+------+ +----+------------+---------+-------+
| 3 | Mimi | | 3 | 1 | ELA | 98 |
+----+------+ +----+------------+---------+-------+
| 4 | 2 | Biology | 92 |
+----+------------+---------+-------+
| 5 | 2 | Chem | 94 |
+----+------------+---------+-------+
| 6 | 2 | Math | 98 |
+----+------------+---------+-------+
| 7 | 3 | Math | 100 |
+----+------------+---------+-------+
I would like to select all subjects and grades from a random student that is enrolled in more than three subjects. (Either Dave or Kate)
Students John and Mimi would not be even considered because they are not enrolled in three subjects.
I know I can achieve this with PHP but I would like this to be done with one query to the database.
SELECT * FROM Students t JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM Students) AS x ON t.ID >= x.ID LIMIT 1
With the above query, I have selected a random student, with that I can go in and check if they have three subjects with SELECT count(subjects) FROM Students WHERE id=random_id.
If the count returned is below three, then I throw away the results and run the first query again.
How would I attempt this in one query?
This is tested and working:
SELECT *
FROM Students s
JOIN (
SELECT student_id
FROM Student_Grades
GROUP BY student_id
HAVING COUNT(*) >= 3
ORDER BY RAND()
LIMIT 1
) rs
ON rs.student_id = s.id
JOIN
Student_Grades sg
ON sg.student_id = s.id
Here's the SQL Fiddle: http://sqlfiddle.com/#!2/e5b5b/1