How to update id from Temp table to Actual Table - php

I have two table, Temp_Table and Actual_Table. I upload csv file that only contain field home_id for the first time and will insert to Temp_Table then insert to Actual_Table. Field id is auto incremented and home_run is field that will generate after user input process.
This is the code i use to generate home_run based on home_id then insert to actual table.
$query1 = "UPDATE Temp_Table AS p JOIN (
SELECT t.id,(SELECT count(*) FROM Temp_Table
WHERE home_id = t.home_id AND id <= t.id) AS home_run FROM Temp_Table t)
AS g ON p.id = g.id SET p.home_id = g.home_id";
$query2 = "INSERT INTO Actual_Table (home_id, home_run) SELECT (home_id, home_run) FROM Temp_Table";
$query3 = "SELECT MAX(home_run) FROM Actual_Table GROUP BY home_id";
This is example of data in Actual_Table after first time i upload the csv file.
Data in Actual_Table (Table 1)
+---------------------------+
|id | home_id | home_run |
|1 | H01 | 1 |
|2 | H01 | 2 |
|3 | H01 | 3 |
|4 | H02 | 1 |
+---------------------------+
Let say, i upload csv file for second time and this is the data in Temp_Table.
Example Data in Temp_Table (Table 2)
+---------------------------+
|id | home_id | home_run |
|1 | H01 | 1 |
|2 | H01 | 2 |
|3 | H02 | 1 |
+---------------------------+
I need to insert value from Temp_Table to Actual_Table and continuously count the home_run. Example from the Table 1, i get the highest home_run for each id. H01 = 3, H02 = 1.
So i plan to do calculation based on home_id: (Highest home_run) 3 + (Temp_Table home_run) x. x is 1 and 2 for H01 and x is 1 for H02.
At the end, the Actual_Table will be like this
ORDER BY id
+---------------------------+
|id | home_id | home_run |
|1 | H01 | 1 |
|2 | H01 | 2 |
|3 | H01 | 3 |
|4 | H02 | 1 |
|5 | H01 | 4 |
|6 | H01 | 5 |
|7 | H02 | 2 |
+---------------------------+
ORDER BY home_run
+---------------------------+
|id | home_id | home_run |
|1 | H01 | 1 |
|2 | H01 | 2 |
|3 | H01 | 3 |
|5 | H01 | 4 |
|6 | H01 | 5 |
|4 | H02 | 1 |
|7 | H02 | 2 |
+---------------------------+
It is possible to do this? I am stuck and do not have idea to create the query for the calculation.

Below are my ways of achieving this but I'm sure there are better solutions out there.
1st of all, get the maximum ID out of actual table (Mark as A):
SELECT MAX(id) FROM Actual_Table
Then, get the maximum home_run of each home_id (Mark as B):
SELECT MAX(home_run) FROM Actual_Table Group By home_id
Finally, join them together into a SELECT statement:
SELECT MAX(A.id) + T.id, T.home_id, T.home_run + MAX(B.home_run)
FROM Temp_Table T, Actual_Table A, Actual_Table B
WHERE T.home_id = B.home_id
GROUP BY T.id, T.home_id, T.home_run
The rest is just simply changing the SELECT statement into INSERT statement.
INSERT INTO Actual_Table
SELECT MAX(A.id) + T.id, T.home_id, T.home_run + MAX(B.home_run)
FROM Temp_Table T, Actual_Table A, Actual_Table B
WHERE T.home_id = B.home_id
GROUP BY T.id, T.home_id, T.home_run

Related

How to count rows with the same value but different id

I have used this sql statement in my database that uses COUNT and UNION ALL
SELECT tem.book_id, COUNT( * )
FROM(SELECT book_id FROM borrowdetails
WHERE borrowdetails.borrow_status = 'returned'
UNION ALL
SELECT book_id from reserve) as tem
GROUP BY book_id
ORDER BY book_id DESC
then i used this code to name the id in the PHP table
while($row=mysql_fetch_array($user_query)){
$id=$row['book_id'];
$book_query = mysql_query("select * from book where book_id = '$id'")or die(mysql_error());
}
it works fine but it doesn't unite the data with the same name because it has different id's like this
+------+------+-------+
| id | name | count |
+------+------+-------+
|1 |A | 3 | the id here is not shown at the table in PHP just
+------+------+-------+ inside the database but the A is duplicate.
|2 |A | 1 |
+------+------+-------+
|3 |B | 2 |
+------+------+-------+
my desired output should be like this without the id showing in the table.
+------+-------+
| name | count |
+------+-------+
|A | 4 |
+------+-------+ this is the table that should be shown in PHP
|B | 2 |
+------+-------+
as you could see the name A's count becomes four because i want also to add the COUNT(*) of both A's.
What am i going to do in order to achieve the desired outcome?
btw this is the db tables that are used.
+------+------+ +---------+---------+ +-------+-------+
| book table | |borrowdetails table| | reserve table |
+------+------+ +---------+---------+ +-------+-------+
| id | name | |brw_dt_id| book_id | |res_id |book_id|
+------+------+ +---------+---------+ +-------+-------+
|1 |A | | 1 | 2 | | 1 | 1 |
+------+------+ +---------+---------+ +-------+-------+
|2 |A | | 2 | 3 | | 2 | 1 |
+------+------+ +---------+---------+ +-------+-------+
|3 |B | | 3 | 3 | | 3 | 1 |
+------+------+ +---------+---------+ +-------+-------+
You would want to use book_name rather than using book_id.
I haven't tried this, but it should work
Select book_name, count(*) as cnt from books
where book_id IN (
SELECT book_id FROM borrowdetails
WHERE borrowdetails.borrow_status = 'returned'
UNION ALL
SELECT book_id from reserve
)
GROUP BY Book_Name
ORDER BY Cnt
We would normally write that this way
SELECT book_name
, count(*) cnt
FROM books x
JOIN
( SELECT book_id
FROM borrowdetails
WHERE borrowdetails.borrow_status = 'returned'
UNION ALL
SELECT book_id from reserve
) y
ON y.book_id = x.book_id
GROUP BY Book_Name -- this assumes that book_name is unique
ORDER BY Cnt

After applying joins in mysql how to get all columns with field is null or empty

My first table structure and data like this (allowance):
-------------------------
|Sno | allwnce |
------------------------|
|1 | HRA |
------------------------|
|2 | CA |
------------------------|
|3 | TA |
------------------------|
|4 | SA |
------------------------
second table structure and data like this (save_employee_allowance):
----------------------------------------------
|Sno | EmpID | allowanceName| amount |
----------------------------------------------|
|1 | EM-1001 | HRA | 1200 |
----------------------------------------------|
|2 | EM-1001 | CA | 800 |
----------------------------------------------|
I want like this :
----------------------------------------------
|Sno | EmpID | allowanceName| amount |
----------------------------------------------|
|1 | EM-1001 | HRA | 1200 |
----------------------------------------------|
|2 | EM-1001 | CA | 800 |
----------------------------------------------|
|3 | EM-1001 | TA | NULL |
----------------------------------------------|
|4 | EM-1001 | SA | NULL |
----------------------------------------------|
I am trying left join like this :
Select *
From save_employee_allowance
LEFT JOIN allowance
ON allowance.allwnce = save_employee_allowance.allowanceName
Where EmpID = 'EM-1001'
But it only give result for matching allowance name
If you want all rows in allowance, then make that the first table in the LEFT JOIN. In addition, you then need to move the WHERE condition to the ON clause:
Select a.Sno, coalesce(em.EmpID, 'EM-1001') as EmpId, a.allowanceName, ea.amount
From allowance a LEFT JOIN
save_employee_allowance ea
ON a.allwnce = ea.allowanceName and ea.EmpID = 'EM-1001';
Hi you have to place allowance on left side beacuse LEFT JOIN keyword returns all rows from the left table (allowance), with the matching rows in the right table (save_employee_allowance). The result is NULL in the right side when there is no match.
select *
From allowance a LEFT JOIN
save_employee_allowance ea
ON a.allwnce = ea.allowanceName where ea.EmpID = 'EM-1001';

Retrieve unique results from JOIN query

I have two tables, one is 'posts' the other is 'images'
posts table
id | article |
1 | post 1 |
2 | post 2 |
3 | post 3 |
images table
project_id | image_name|
1 | img1 |
1 | img2 |
2 | img3 |
2 | img4 |
3 | img5 |
3 | img6 |
My current query is this:
SELECT * FROM `images` RIGHT JOIN `posts` ON images.project_id = posts.id
But it is showing multiple results from the posts table.
I want the results like this, to avoid multiple results.
post 1 - img1
post 2 - img3
post 3 - img5
SELECT
p.article, i.image_name
FROM
`posts` p
JOIN (
select
i2.project_id, min(i2.image_name) as image_name
from
images i2
group by
i2.project_id
) i
on i.project_id=p.id
SELECT * FROM posts RIGHT JOIN images ON posts.id = images.project_id group by posts.id
Use group by Clasue to get unique result.
This will do exactly what you asked for.
SELECT `posts`.id, `posts`.`article`,`images`.`image_name` FROM `posts` INNER JOIN `images` ON `posts`.`id` = `images`.`project_id` GROUP BY id
Check this out.
MariaDB [fbb]> SELECT * FROM `posts`;
+----+---------+
| id | article |
+----+---------+
| 1 | post1 |
| 2 | post2 |
| 3 | post3 |
+----+---------+
3 rows in set (0.00 sec)
MariaDB [fbb]> SELECT * FROM `images`;
+------------+------------+
| project_id | image_name |
+------------+------------+
| 1 | img1 |
| 1 | img2 |
| 2 | img3 |
| 2 | img4 |
| 3 | img5 |
| 3 | img6 |
+------------+------------+
6 rows in set (0.00 sec)
MariaDB [fbb]> SELECT `posts`.id, `posts`.`article`,`images`.`image_name` FROM `posts` INNER JOIN `images` ON `posts`.`id` = `images`.`project_id` GROUP BY id
-> ;
+----+---------+------------+
| id | article | image_name |
+----+---------+------------+
| 1 | post1 | img1 |
| 2 | post2 | img3 |
| 3 | post3 | img5 |
+----+---------+------------+
3 rows in set (0.00 sec)
MariaDB [fbb]>

PHP MySQL count events per location per user

I'm trying to build a query using data from 4 tables: Bookings, Users, Events, Locations
Bookings :
+---------------------------------+
|book_id | event_id | person_id |
+---------------------------------+
|1 | 1 | 2 |
|2 | 2 | 1 |
|3 | 2 | 2 |
|4 | 1 | 3 |
|5 | 3 | 1 |
|6 | 3 | 2 |
+---------------------------------+
Users :
+----------------------+
| user_id | name |
+----------------------+
| 1 | Joe |
| 2 | Jack |
| 3 | Jane |
+----------------------+
Events :
+------------------------+
| event_id | location_id |
+------------------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
+------------------------+
Locations :
+---------------------------+
| location_id | name |
+---------------------------+
| 1 | Lombard |
| 2 | NYC |
| 3 | LA |
+---------------------------+
The query that I can't seem to write should get me to display a table like this :
+------------------------------+
+Name |Lombard|NYC|LA|Total|
+------------------------------+
+Joe |1 |0 |1 |2 |
+Jack |2 |0 |1 |3 |
+Jane |1 |0 |0 |1 |
+------------------------------+
+Totals |4 |0 |2 |6 |
+------------------------------+
What I got to work is displaying how many booking have been made per user but not per user AND per location using this query:
$query='
SELECT
bookings.person_id,
COUNT(bookings.person_id) AS total,
bookings.event_id,
users.display_name
FROM bookings
INNER JOIN users ON bookings.person_id=users.id
WHERE users.id=bookings.person_id
GROUP BY bookings.person_id';
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_array($result))
{
/* total bookings per user */
$value = $row['total'];
$sum += $value;
/* Displaying results */
echo "<tr width='500'>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['display_name']."</td>";
echo "<td>".$row['total']."</td>";
echo "</tr>";
}
This works okay and displays :
+-----------------------------------+
| ID | NAME | Total Bookings |
+-----------------------------------+
| 7 | Bob | 3 |
| 5 | Jane | 2 |
| 3 | Joe | 1 |
+-----------------------------------+
Could you please help me getting there. Thanks.
You are looking for a pivot table style query. Here's one way you can do it.
select u.name,
count(case when l.name = 'Lombard' then 1 end) as lombard,
count(case when l.name = 'NYC' then 1 end) as nyc,
count(case when l.name = 'LA' then 1 end) la,
count(u.name) total
from bookings b
inner join events e
on b.event_id = e.event_id
inner join locations l
on e.location_id = l.location_id
inner join users u
on u.user_id = b.person_id
group by u.name
with rollup
fiddle here
It gets a lot harder (and is generally easier to do in the application) if you dont know the possible column (location) values when you are writing the query.
http://sqlfiddle.com/#!9/92d50
SELECT u.name,
SUM(l.name = 'Lombard') lombard,
SUM(l.name = 'NYC') nyc,
SUM(l.name = 'LA') la,
COUNT(*) total
FROM bookings b
LEFT JOIN `events` e
ON b.event_id = e.event_id
LEFT JOIN locations l
ON e.location_id = l.location_id
LEFT JOIN users u
ON u.user_id = b.person_id
GROUP BY u.name
WITH ROLLUP

Multiple group in mysql

I have table, named "table_log".
Here is the structure
---------------------------------------
|id_log | user_id | login_date |
---------------------------------------
|1 | 1 |2014-09-02 14:58:53 |
|2 | 1 |2014-09-03 24:18:53 |
|3 | 1 |2014-09-02 14:58:53 |
|4 | 1 |2014-09-01 02:28:53 |
|5 | 2 |2014-09-04 01:48:53 |
|6 | 3 |2014-09-05 04:58:53 |
|7 | 2 |2014-09-06 03:58:53 |
----------------------------------------
I want to count number of user each days. not how much log is.
As an example data, I want to show it like this:
---------------------------
|date | user_number|
---------------------------
|2014-09-02 | 1 |
|2014-09-03 | 1 |
|2014-09-04 | 5 |
---------------------------
Does any can help me? How to query my database?
SELECT date(login_date) AS date, count(DISTINCT user_id) AS user_count
FROM table_log
GROUP BY date(login_date)
The date function gives just the date-part of a datetime column, then it's a simple group by.
I dont get your ques, but u expect it?
2014-09-03 there is one record how user number will come 4 ? which scenario u asking?
SELECT date(login_date) AS date, count(date(login_date))
FROM tbl
GROUP BY date(login_date)

Categories