Multiple tables from two database in mysql using php - php

I have 2 tables in Mysql db, table 1
serial Remark
-------------------
1 programming in c++
2 OOPS using java
and table 2
Name date code serial
-------------------------------------
Jack 2014-10-07 c++ 1
Jill 2014-10-07 c++ 1
Moos 2014-10-07 c++ 1
Jack 2014-10-08 java 2
Jill 2014-10-08 java 2
I want to generate multiple table using php in HTML,
1.Students in Programming in c++ are
Name date
-------------------
Jack 2014-10-07
Jill 2014-10-07
Moos 2014-10-07
2.Students in OOPS using java
Name date
-------------------
Jack 2014-10-08
Jill 2014-10-08
Can someone suggest the query?

Query for case 1:
SELECT name, date from table2 inner join table1 on table2.serial = table1.serial where Remark='programming in c++'
Query for case 2:
SELECT name, date from table2 inner join table1 on table2.serial = table1.serial where Remark='OOPS using java'
or,
Another way:
Query for case 1:
SELECT name, date from table2, table1 where table2.serial = table1.serial and Remark='programming in c++'
Query for case 2:
SELECT name, date from table2, table1 where table2.serial = table1.serial and Remark='OOPS using java'

Related

get all the tables within a date range

I have many weekly backup tables of the same structure and columns but with different names containing some user-specific information like this.
user_details
user_details_20211126
user_details_20211119
user_details_20211112
and so on....
Now I want to find the tables within a date range for example: If I choose the date range of 2021-11-20 to 2021-11-13 then the tables fetched would be user_details, user_details_20211126, user_details_20211119 for using them in a query containing union operation on all the fetched tables.
Any idea on how to do that?
As already commented, what an awful idea ... you shouldn't be doing it that way. One table with a DATE datatype column should by the way to do it. I suggest you switch to it - the sooner, the better.
Meanwhile - although you didn't name database you use & I'm using Oracle - here's what you'll probably have to do, regardless of the database: some kind of dynamic SQL as you have to "dynamically" compose the SELECT statement, using only tables that satisfy the condition. As you have to fetch their names from the data dictionary, you don't have many options but - as I said - dynamic SQL.
OK, here you go.
Setting date format (you don't have to do that):
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
Tables whose names are user_details_something:
SQL> select table_name
2 from user_tables
3 where table_name like 'USER_DETAILS%'
4 order by table_name;
TABLE_NAME
------------------------------
USER_DETAILS
USER_DETAILS_20211126
USER_DETAILS_20211127
USER_DETAILS_20211128
How to select only tables whose name contains date that is between desired values?
query data dictionary (in Oracle, that's user_tables)
extract the DATE part of their names (I chose regexp_substr function; you'd use any you find appropriate)
convert extracted substring into a valid date value (using TO_DATE function)
Therefore:
SQL> select table_name
2 from user_tables
3 where table_name like 'USER_DETAILS%'
4 and to_date(regexp_substr(table_name, '\d+$'), 'yyyymmdd')
5 between date '2021-11-26' and date '2021-11-27';
TABLE_NAME
------------------------------
USER_DETAILS_20211126
USER_DETAILS_20211127
OK; two tables between 26.11.2021 and 27.11.2021 (I didn't feel like creating more of them).
The second part of the job is to actually fetch data from those tables. Here's a function that accepts date period as parameters and returns ref cursor (you can choose something else, of course). Read comments within code:
SQL> create or replace function f_test(par_date_from in date,
2 par_date_to in date)
3 return sys_refcursor
4 is
5 l_str varchar2(1000); -- contains the whole SELECT statement
6 rc sys_refcursor;
7 begin
8 -- loop through all tables whose names satisfy the condition
9 for cur_r in
10 (select table_name
11 from user_tables
12 where table_name like 'USER_DETAILS%'
13 and to_date(regexp_substr(table_name, '\d+$'), 'yyyymmdd')
14 between par_date_from and par_date_to
15 ) loop
16 -- compose a SELECT statement
17 l_str := l_str ||
18 'select ename, job, datum from ' || cur_r.table_name || ' union all ';
19 end loop;
20
21 -- remove trailing UNION ALL
22 l_str := rtrim(l_str, ' union all');
23
24 -- open and return ref cursor
25 open rc for l_str;
26 return rc;
27 end;
28 /
Function created.
OK, let's try it:
SQL> select f_test(date '2021-11-26', date '2021-11-27') from dual;
F_TEST(DATE'2021-11-
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
ENAME JOB DATUM
---------- --------- ----------
SMITH CLERK 26.11.2021
JONES MANAGER 26.11.2021
SCOTT ANALYST 26.11.2021
ADAMS CLERK 26.11.2021
FORD ANALYST 26.11.2021
ALLEN SALESMAN 27.11.2021
WARD SALESMAN 27.11.2021
MARTIN SALESMAN 27.11.2021
BLAKE MANAGER 27.11.2021
TURNER SALESMAN 27.11.2021
JAMES CLERK 27.11.2021
11 rows selected.
SQL>
It works for me; I hope you won't have to make it "work" at all, but - after you apply what we've already suggested - run a simple
SQL> select * from user_details
2 where datum between date '2021-11-26' and date '2021-11-27';
DEPTNO EMPNO ENAME JOB DATUM
---------- ---------- ---------- --------- ----------
20 7369 SMITH CLERK 26.11.2021
20 7566 JONES MANAGER 26.11.2021
20 7788 SCOTT ANALYST 26.11.2021
20 7876 ADAMS CLERK 26.11.2021
20 7902 FORD ANALYST 26.11.2021
30 7499 ALLEN SALESMAN 27.11.2021
30 7521 WARD SALESMAN 27.11.2021
30 7654 MARTIN SALESMAN 27.11.2021
30 7698 BLAKE MANAGER 27.11.2021
30 7844 TURNER SALESMAN 27.11.2021
30 7900 JAMES CLERK 27.11.2021
11 rows selected.
SQL>
Obviously, my user_details table contains the DATE datatype column and makes everything A LOT simpler.

Complex Queries range dates

mySQL dateTime range Query Issue
how get count of proceser in 2017 by same date like 2017-08-07
date | name
-----------------------
2017-08-31 | amr
-----------------------
2017-08-05 | ahmed
----------------- -----
2018-08-08 | moh
how get 2017-01-01 BETWEEN 2017-12-31
------------------------
count | date
-----------------------
2 | 2017
-----------------------
1 | 2018
SELECT count(*)
FROM item WHERE
date IN
( SELECT date
FROM item WHERE
(BETWEEN '2017-03-15' AND '2017-09-31'))
I couldn't find a duplicate for you, but I am sure there is one somewhere.
If you use GROUP BY and the DATE_FORMAT() function, you can COUNT() the occurrences in each group.
SELECT COUNT(*),DATE_FORMAT(`date`,'%Y') FROM `item` GROUP BY DATE_FORMAT(`date`,'%Y');
You can include a WHERE clause before GROUP BY if you need to omit certain ranges of time.
1, date is a reserved MySQL word. It would be best to name your column something else, but you can use it if you also specify the table name like table.date
2, there is no need for a sub-query. MySQL can do this in one query
SELECT count(`table.name`) as NUMBER, YEAR(`table.date`) as YR FROM item WHERE DATE(`table.date`) BETWEEN '2017-01-01' AND '2017-12-31'
BETWEEN is kind of a "ternary" operator, of the form x BETWEEN y AND z
You want:
`date` BETWEEN '2017-03-15' AND '2017-09-31'

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

MySQL query problematic

I'm using PHP/MySQL and I have to create a new table from an existing one. Here's the problematic:
Table1:
photo_id email name_to creation_date link
1 foo1#bar.com paul 2012-11-21 link1.com
2 foo2#bar.com mark 2012-11-22 link2.com
3 foo1#bar.com alex 2012-11-23 link3.com
4 foo1#bar.com saul 2012-11-25 link4.com
5 foo1#bar.com john 2012-11-26 link5.com
6 foo2#bar.com math 2012-11-27 link6.com
7 foo3#bar.com fred 2012-11-28 link7.com
In Table1 the email is not unique, it can be repeated several times. Each link is different. With this data, I have to create a new table in which the email is unique with maximum of 3 links if there's more entries of one email (if so I need the data of the 3 latest entries).
So, in this case, Table2 would be like:
email link1 creation_date1 name_to1 link2 creation_date2 name_to2 link3 creation_date3 name_to3
foo1#bar.com link5.com 2012-11-26 john link4.com 2012-11-25 saul link3.com 2012-11-23 alex
foo2#bar.com link6.com 2012-11-27 math link2.com 2012-11-22 mark
foo3#bar.com link7.com 2012-11-28 fred
I know the GROUP_CONCAT feature but it's not really what I need here since the links would all be in the same column. Is it better to make a SELECT * FROM table1 and process the result into PHP arrays and after that create Table2 or a unique MySQL query would do the trick? Or create multiple MySQL tables?
Table1 is over 10 millions rows.
Any advice would be appreciate.
Thanks.
1) select all unique emails.
2) For each email, take the first 3 rows with that email ordered by creation_date descending.
3) Use that data to insert into new table.
Whatchu think?

Categories