Issue while copying tables on PHP SQL - php

All id are auto_increment. I will try explain my issue understandable. I just want copy 2 tables to another 2 tables. Here my tables at below:
table1
id number
10 100
11 102
12 105
13 106
table2
id number_id subnumber
52 10 10
53 11 15
54 13 40
You can see there is subnumber of some numbers. For example, WHERE id =11 from table1 has a subnumber on table2 and it is equal to 15. Now I should copy the table1 to the table : copy_table1
$sql1 = mysql_query('INSERT INTO
copy_table1 (copy_number)
SELECT
number
FROM
table1');
And the results of copy_table1 are:
copy_table1
id copy_number
100 100
101 102
102 105
103 106
Then I should copy table2 to another table copy_table2:
$sql2 = mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT
number_id, copy_subnumber
FROM
table2');
And the results of copy_table2 are:
id copy_number_id copy_subnumber
60 10 10
61 11 15
62 13 40
So, lets check the tables: copy_table1 and copy_table2.
You see, ID from copy_table1 is not equal and suitable to copy_number_id FROM copy_table2
And it brings me problem. How can I do that after copying tables id and copy_number_id will be suitable to each other?

$sql2=mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT DISTINCT
cp1.id, t2.subnumber
FROM
copy_table1 AS cp1
CROSS JOIN
table1 AS t1
USING (number)
INNER JOIN
table2 AS t2
ON
t1.id = t2.number_id ');

Related

how to generate students mark ledger from 3 tables- Student,Mark,Subject

I have four tables
1.student-it contains id name etc
2.marks - it contains markid,student_id,batch,class,section,subject_id,exam_id,obt_mark
3.subject table - it contains sub_id,sub_name
4. exam_type- examtype_id, exam_name
i need mark ledger for batch-2014,class=5,section=1,exam_id=3 as shown below-
NAME ROLL NO SUB1 SUB2 SUB3 TOTAL RESULT POSITION RANK
RAM 0054 65 54 64 183 PASS FIRST 2
HAri 0054 65 54 65 184 PASS FIRST 1
gopal 0054 65 50 65 180 PASS FIRST 3
saroj 0054 65 44 65 174 PASS FIRST 4
Thanks in advance .
Here i have sql query
SELECT `marklists`.`mrk_sub_id1`, `marklists`.`mrk_marks`, `marklists`.`mrk_practical`,
`marklists`.`mrk_exam_type`, `students`.`id` as st_id, `students`.`st_roll`,
`students`.`st_name`, `students`.`batch`, `students`.`st_class`,
`students`.`st_section`, `courses`.`id`, `classes`.`class_name` as class_name,
`courses`.`sb_name` as subject_name, `courses`.`sb_fullmark` as full_marks,
`courses`.`sb_passmark` as pass_marks FROM (`marklists`)
LEFT JOIN `classes` ON `marklists`.`mrk_class` = `classes`.`id`
LEFT JOIN `students` ON `marklists`.`mrk_student_id`=`students`.`id`
LEFT JOIN `courses` ON `marklists`.`mrk_sub_id`=`courses`.`id`
WHERE `marklists`.`mrk_exam_type` = '3' AND `marklists`.`mrk_batch` = '3'
AND `marklists`.`mrk_class` = '1' AND `marklists`.`mrk_section` = '1'
and I need ledger like above any help??
Join the tables based on common key and for the titles which you need as subject you can use pivot operator to convert rows to column and use where clause for rest of the conditions
hope this helps.

inserting values to new table in mysql_query

Okay guys, I will try to share my guestion understandable. So I have 2 tables like below:
ALL Id from TABLES are Autoincrement.
Number
id number
10 100
11 102
12 105
13 106
subnumber
id number_id subnumber
52 10 10
53 11 15
54 13 40
You see some numbers (not all) have subnumbers. I mean:
From table number the number where id = 13 has subnumber and it is equal to 40.
I want to save this table in another table.
$sql1=mysql_query('SELECT number FROM number');
$while(fetch1=mysql_fetch_array($sql1))
{
mysql_query('
INSERT INTO `save_number` (number)'
VALUES ('.$fetch1['number'].');
);
}
save_number
id number
100 100
101 102
102 105
103 106
Now i gonna save table (subnumber) in another table (save_subnumber)
$sql2=mysql_query('SELECT number_id, subnumber FROM subnumber');
$while(fetch2=mysql_fetch_array($sql2))
{
mysql_query('
INSERT INTO `save_subnumber` (number_id, subnumber)'
VALUES ('.$fetch2['number_id'].', '.$fetch2['number'].');
);
}
save_subnumber
id number_id subnumber
60 10 10
61 11 15
62 13 40
So, you see the number_id FROM save_subnumber is not equal to new inserted id FROM save_number. I would be appreaciated if anyone help me. By the way I am still using mysql_query. I can not find time to improve my SQL to PDO :)
Your first query can be changed to
INSERT INTO
save_number (number)
SELECT
number
FROM
number
This will save you using PHP to iterate through rows and will be faster. A lot.
Having this in mind your second query would be
INSERT INTO
save_subnumber (number_id, subnumber)
SELECT DISTINCT
sn.id, s.subnumber
FROM
saved_number AS sn
CROSS JOIN
number AS n
USING (number)
INNER JOIN
subnumber AS s
ON
n.id = s.number_id
If you just want to copy one table in another why don't you use something like this
INSERT INTO save_number SELECT * FROM number;

mysql, php, how to compare numbers

i have a database: database with a table: table and some fields:
id fname dphone count_pic dup_id
6055903 Karla 5126xxx798 1 57
6173767 Aaliyah 4082xxx534 4 39
5611411 Aaliyah 4082xxx534 15 39
5611211 Aaliyah 4082xxx534 18 39
4234798 Abby 3057xxx974 31 16
6166691 Walter 6178xxx280 1 74
3375576 Walter 6178xxx280 17 74
what i am trying to do is to select the fields that have the smallest count_pic and the ones that have the bigger count_pic and that have the same dup_id
any ideas how to do this in mysql?
thanks.
With this query you'll select the smallest and the biggest values of count_pic for every dup_id
SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id
If you also need corresponding rows, then you could use something like
SELECT *
FROM `table` t1
INNER JOIN (SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id
AND (t1.count_pic = minpic
OR t1.count_pic = maxpic)

Selecting from 2 tables in a single query

I have a table that I'm querying for value 43 in the second field and I return the value of the third field
SELECT t1_field3 FROM table1 WHERE t1_field2=43
this returns 19, 39,73
t1_id t1_field2 t1_field3
----- --------- ---------
1 43 19////
2 43 39////
3 43 73////
4 73 43
5 13 40
Then I separately query a second table for additional information
SELECT * FROM table2 WHERE t2_id=t1_field3
t2_id t2_field2 t2_field3
----- --------- ---------
19 value19.2 value19.3
39 value39.2 value39.3
73 value73.2 value73.3
Is there a way I could combine both table1 and table2 in the same query?
You're describing a JOIN. In this case you don't need to explicitly use the JOIN keyword though, you can just do this:
SELECT table1.t1_field3, table2.* FROM table1, table2 WHERE table1.t1_field2=43 AND table2.t2_id = table1.t1_field3
It would probably be helpful to learn about the different types of joins at some point; Coding Horror has a good post about it
select * from table2
where t2_id in (select t1_field3 from table1 where t1_field2=43 )
There's a way to express the join directly as well like so:
select table1.t1_field3, table2.*
from table1
join table2 on table1.t1_field3 = table2.t2_id
where table1.t1_field2 = 43;

summing two columns total in mysql

I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.

Categories