Simple MySQL Two Table Query [duplicate] - php

This question already has answers here:
MySQL join with where clause
(3 answers)
Closed 5 years ago.
I have two tables
1st table :-
id name dept
1 John dept1
2 Mary dept2
3 Dave dept3
4 John dept4
5 John dept5
2nd table :-
id submitter dept
1 Rupert dept3
2 Joe dept1
3 Lisa dept2
4 Louise dept4
5 Tom dept5
what i would like is a query to allow people in the name column in the first table to only show records based on their matching departments eg John in table one will return the 3 records in table 2 (id 2,4 and 5)
So far i have tried SELECT * FROM table1, table2 WHERE table1.dept = table2.dept AND table1.name='John'

If I correctly understand your problem, you need to make a join between the two tables using the field dept and filter your results by the name of the requester, in the first table.
SELECT t2.submitter, t2.dept
FROM table1 t1
LEFT JOIN table2 t2 ON t1.dept = t2.dept
WHERE t1.name = :person_name
Documentation and examples (you can also look at the left menu at Inner, right, full and self join's).

Related

Multiple MySQL Table Query

I could not find an answer by searching as I am not sure what exactly it would be called what I'm searching for.
Anyways, I have multiple tables in MySQL and am trying to "fill in" some of the final product.
myTable
id assigned_to location
1 2 3
2 2 3
3 3 3
myUsers
id name
1 John
2 David
3 Sally
myLocation
id name
1 SAT
2 DEN
3 AUS
Basically the end product should pull the "myTable" data and fill into a table (which I already know how to do) the name and location of each row/column so that it states something alongm the lines of
ID Assigned To Location
1 David SAT
Instead of
ID Assigned To Location
1 2 2
This should produce the expected result:
SELECT mt.id, mu.name, ml.name
FROM mytable mt JOINT myUsers mu ON mt.assigned_to = mu.id
JOIN myLocation ml ON mt.location = ml.id

Connecting 3 tables to get data [duplicate]

This question already has answers here:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]
(3 answers)
Closed 8 years ago.
I have 3 tables - User table, book1 table, book2 table.
User table is like this -
user_id | gender | l_name | f_name
-------- -------- -------- -------
1 male Doe Jon
2 female Xu Jini
3 female Din Jane
book1 table -
b_id | user_id | amount | date
----- --------- -------- ----------
1 3 98.30 2014-05-14
2 1 65.70 2014-05-07
3 2 14.40 2014-05-06
4 2 55.60 2014-05-07
book2 table -
b_id | user_id | amount | date
----- --------- -------- ----------
1 2 38.20 2014-04-06
2 3 84.40 2014-04-02
3 3 31.30 2014-04-12
4 1 74.40 2014-05-06
The user gives a date range as input and I want to calculate the sales count(COUNT), total amount(SUM) and the max date(MAX) for that date range. After this I want to connect this data to the user table and get the gender and name using the user_id.
I wrote this query to get the data for the given date range from book1 and book2 tables-
SELECT * FROM book1
WHERE date between '2014-04-02' and '2014-05-15'
UNION ALL
SELECT * FROM book2
WHERE date between '2014-04-02' and '2014-05-15'
ORDER BY customer_id;
By this i get all the rows in the book1 and book2 table which satisfy the date range. Now should i use subquery or something else to reach the goal. I think sql should take care till getting the count, sum and max from book tables. Then the connection to the user table should be done in PHP. Am i on the right path? Can everything be done in SQL? I am kinda lost.
Yes, you can do it in SQL using a plain JOIN.
This will basically get all users and join them up with their respective amounts in the period. After that, the results are grouped by user so that we can sum up the amounts.
SELECT u.user_id, u.l_name, u.f_name, SUM(x.amount) `total amount`
FROM user u
JOIN (
SELECT user_id, date, amount FROM book1
UNION ALL
SELECT user_id, date, amount FROM book2
) x
ON u.user_id = x.user_id
AND x.date between '2014-04-02' and '2014-05-15'
GROUP BY u.l_name, u.f_name,u.user_id
An SQLfiddle to test with.
As a side note, learning about joins is really a necessity to work efficiently with SQL databases.

Php mysql routine for calculating in one table and place results in another table

I need a php routine for my mysql database. I have made an example to illustrate the problem:
Lets say I have a table that registrates customers and how much money they spend. A customer can have more registrations:
Table1:
Name - Amount
Jane - 3
Mark - 4
Sara - 5
Jane - 5
Jane - 6
Sara - 2
I want a routine that goes trough Table1, and finds how much each person has spend. I want the result in Table2, like this:
Table2:
Jane - 14
Mark - 4
Sara - 7
Do you have a solution to this?
insert into table2 select name, sum(amount) from table1 group by name;
SELECT Name, SUM(Amount) FROM Table1 GROUP BY Name

How to collect IDs and group them comma separated from multiple connected tables in MySQL [duplicate]

This question already has answers here:
Create Comma Seperated List from MySQL PHP
(3 answers)
Closed 9 years ago.
I am trying to pull IDs and display them comma separated from multiple connected tables, example and structure below, I think so I must use concat and group by but not quite sure how?
table_1
ID NAME
-- -----
1 Test1
2 Test2
table_2
ID TABLE_1_ID Name
-- ---------- --------
1 1 abc
2 1 abcd
3 1 abcde
4 2 abcdef
5 2 abcdefg
6 2 abcdefgh
And I would like to get result:
Test1 = abc,abcd,abce
Test2 = abcdef, abcdefg, abcdefgh
You can do it using the GROUP_CONCAT function.
select table_1.NAME, GROUP_CONCAT(table_2.NAME)
from table_1 inner join table_2
on table_1.ID=table_2.TABLE_1_ID
group by table_1.ID

Compare two tables and update second table

There are two tables table1 and table2
table1 has got two columns name and rank
table2 has got only one column name
names in table2 are almost listed in table1
I want compare two table and pull rank info from table1 and update/alter table2 with rank
table1
name | rank
-------------
john | 2
mathews| 5
keyn | 4
emly | 25
yancy | 8
stewart| 9
kim | 12
chris | 19
table2
name
-------
john
mathews
keyn
emly
yancy
stewart
I want update/insert rank details to table2 from table1
thats it and sorry for the confusion
Seem like you want to do something like this:
update table2,table1 set table2.rank=table1.rank where table2.name=table1.name
This will update the second table with ranks from the first table where the names are equal.
Then put auto increament field of table one in table2. and after this apply left join using these id and pull the info
refer this link
http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html

Categories