converting a php array into a database schema - php

Currently I am storing adjacencies in a php file in an array. Here's a sample of it:
$my_neighbor_lists = array(
1=> array(3351=> array (2, 3, 5 , 6, 10)),
2=> array(3264=> array (322, 12, 54 , 6, 10), 3471=>array (122, 233, 35 , 476, 210)),
3=> array(3309=> array (52, 32, 54 , 36, 210), 3469=>array (152, 32, 15 , 836, 10)),
etc
I would like to basically migrate this into a db. Any suggestions on how many table I should have? I am looking at three tables here.

two tables:
1. vertices (id)
2. edgecost (idfrom, idto, time, cost)

Related

Compute Summation on Multiple Aspects

I have a database table that is similar to the following picture. Every time an emission is reported, a row is inserted to the table. The total column is just the addition of CO and CO2 columns. There can be multiple rows for any city on a particular date. The following table represents data for only two dates and three different cities.
For each date, I need to compute totals for CO, CO2, and total columns for each city, then calculate the subtotal of those cities for that date. Finally, combine those subtotals into a grand total. The following picture represents the result I am trying to achieve,
In addition to that, I also need to keep a couple of filters such as result for a certain date range or a certain country, etc. I am planning to query rows and loop over them to compute the summations at the PHP end. This approach is questionable as it does not seem very efficient for a large volume of data. I was wondering if there is any efficient way of doing this type of computation, maybe using complex GROUP BY or temporary table/view at the MySQL end. Please suggest any better way to do it for a table that may grow up to a few millions of rows. Let me also include an array representation of the sample data for convenience,
$sample = [
['id', 'date', 'country', 'city', 'CO', 'CO2', 'total'],
[1, '2020-07-13', 'US', 'Scarsdale', 5, 10, 15],
[2, '2020-07-13', 'US', 'Scarsdale', 10, 10, 20],
[3, '2020-07-13', 'US', 'SF', 5, 15, 20],
[4, '2020-07-13', 'US', 'SF', 15, 25, 40],
[5, '2020-07-13', 'UK', 'London', 10, 15, 25],
[6, '2020-07-12', 'UK', 'London', 10, 20, 30],
[7, '2020-07-12', 'UK', 'London', 5, 5, 10],
[8, '2020-07-12', 'US', 'SF', 10, 20, 30],
[9, '2020-07-12', 'US', 'Scarsdale', 5, 5, 10],
];
Thanks in advance.

how to match two different columns in Mysql which has comma separated values

I have two tables:
CampaignTable
which has following property
id , campaign ,user_group
example would be
1 8867116213 5,11,15,16,18,20
2 8867116214 0,8,22
Then I have another table called User Table
with following property
id emp_id user_group
Example is like this
1 274 0,5,8,9,10,11,21,20
2 275 5,11,20
3 279 19,21,22,25
I have to join this table and create an Array which has campaign wise user
for example for campaign with id 1 it should give me
274, 275
How can I achieve this in Mysql
Thanks
You should definetely normalize your data. For example consider this kind of normalization which renders almost no change to your DB structure:
INSERT INTO CampaignTable
(`campaign`, `user_group`)
VALUES
(8867116213, 5),
(8867116213, 11),
(8867116213, 15),
(8867116213, 16),
(8867116213, 18),
(8867116213, 20),
(8867116214, 0),
(8867116214, 8),
(8867116214, 22)
;
INSERT INTO UserTable
(`emp_id`, `user_group`)
VALUES
(274, 0),
(274, 5),
(274, 8),
(274, 9),
(274, 10),
(274, 11),
(274, 21),
(274, 20),
(275, 5),
(275, 11),
(275, 20),
(279, 19),
(279, 21),
(279, 22),
(279, 25)
;
You could then fetch your data with a query as simple as that:
SELECT c.campaign, GROUP_CONCAT(DISTINCT u.emp_id) FROM CampaignTable c
JOIN UserTable u ON c.user_group = u.user_group
GROUP BY c.campaign
See SQLFiddle

sort multidimensional array by values in PHP

I have an array fetched from mysql database tables of type. I want to sort it in order of particular value.
$arr1=array(array(12, 8, 5, 34),
array(54, 87, 32, 10),
array(23, 76, 98, 13),
array(53, 16, 24, 19));
How can i sort it by value?
Like sorting by 2nd value should result to.
$arr1=array(array(12, 8, 5, 34),
array(53, 16, 24, 19),
array(23, 76, 98, 13),
array(54, 87, 32, 10));
I like to use usort to solve these problems.
$sortKey = 1;
usort($arr1, function($a, $b) use($sortKey){
return $a[$sortKey] - $b[$sortKey];
});
Got to agree with #RocketHazmat, array_multsort is a royal pain in the backside. usort is much easier to follow but I thought I'd have a go anyway:
$sortKey = 1;
array_multisort(array_map(function($v) use($sortKey){
return $v[$sortKey];
}, $arr1), $arr1);
It only took 20 minutes... :(
Here's a demo: http://ideone.com/2rZYIz

Combining several looping MySQL queries in PHP

I have two tables that pertain to this part of a trivia quiz game: scores and quiz. I have nested numerous PHP and MySQL loops, and I'm hoping there's a more elegant way (and hopefully, fewer database queries) to achieve the same results.
Any suggestions greatly appreciated.
Below is my MySQL structure, PHP code and some sample data.
Table quiz holds information for each quiz in the system, one record per quiz:
`uid` int(11) NOT NULL AUTO_INCREMENT,
`category_uid` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`sample` binary(2) NOT NULL,
`difficulty` varchar(65) NOT NULL,
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
PRIMARY KEY (`uid`)
A unique ID, a map to categories table (not relevant here), a name, whether it's a sample quiz or not, a difficulty rating, the date it was first created, and the date it was last updated.
Table scores holds results from when a user takes a quiz, one record per question in the quiz (so, ten rows for a 10-question quiz):
`uid` int(11) NOT NULL AUTO_INCREMENT,
`unique_uid` varchar(255) NOT NULL,
`question_uid` int(11) NOT NULL,
`quiz_uid` int(11) NOT NULL,
`user_uid` int(11) NOT NULL,
`answer` varchar(255) NOT NULL,
`correct` binary(2) NOT NULL,
`points` int(25) NOT NULL,
`time` float(20,1) NOT NULL,
`date_created` datetime NOT NULL,
PRIMARY KEY (`uid`)
A unique ID for the record, a unique ID for the instance of the quiz being taken, a map to questions table (not relevant here), a map to quiz table, a map to users table (not relevant here), the answer given, whether it was correct or not, the point score awarded, the time in seconds it took for a response, and the time stamp of the record.
Since a player can re-play quizzes, each scores.unique_uid points to a single time playing the quiz. This means a player might play quiz.uid=1 five different times, meaning five different score.unique_uid for a total of (5 times X 10 questions =) 50 rows recorded in the scores table.
What I'm trying to do is retrieve all distinct scores.unique_uid records (a list of every quiz the player has a score for) and then look at some tabulated results for each set of 10 records (one quiz, 10 questions).
The way I'm doing it now is:
looping through a query that returns each unique quiz.uid the player has scored
looping through another query that returns the scores.unique_uid for each unique instance of the player scoring that particular quiz quiz.uid
using a third query to retrieve the SUM() data for each set of records with unique scores.unique_uid (I couldn't successfully combined with the DISTINCT query)
Here is my PHP code:
// Query retrieves each unique quiz the player has taken
$ss = sqlQuery("SELECT DISTINCT scores.quiz_uid, quiz.difficulty, quiz.name FROM scores, quiz WHERE scores.user_uid = {$_SESSION['uid']} AND quiz.uid = scores.quiz_uid ORDER BY scores.date_created DESC");
if ( mysql_num_rows( $ss ) > 0 ) {
while ( $row = mysql_fetch_assoc( $ss ) ){
/* Step through each quiz and output name and difficulty */
?>
<h2><?php echo ( $row['name'] ); ?> <span><small>Difficulty: <?php echo( $row['difficulty'] ); ?></small></span></h2>
<?php
// Query retrieves each unique unique_uid scored (each instance of every quiz)
$eqs = sqlQuery("SELECT DISTINCT unique_uid FROM scores WHERE quiz_uid = {$row['quiz_uid']} AND user_uid = {$_SESSION['uid']}");
while ( $eqsrow = mysql_fetch_assoc( $eqs ) ){
/* Step through each quiz played instance, and output score and other details */
// Query retrieves SUM()s for total time, total points, total correct responses
$euqs = sqlQuery('SELECT date_created, ' .
'SUM(time) AS times, SUM(points) AS points, SUM(correct) AS ttlcorrect ' .
"FROM scores WHERE unique_uid = {$eqsrow['unique_uid']} AND user_uid = {$_SESSION['uid']} ");
// Output the score
while ( $euqsrow = #mysql_fetch_assoc( $euqs ) ){
?>
<div class="row">
<span class="score"><?php echo( number_format( $euqsrow['points'], 0) ); ?> points</span>
<span class="time"><?php echo( number_format( $euqsrow['times'], 0) ); ?> seconds</span>
<span class="correct"><?php echo( number_format( $euqsrow['ttlcorrect'], 0) ); ?> / 10 correct</span>
<span class="date">Played <?php echo( date( 'F j, Y', strtotime($euqsrow['date_created']) ) ); ?></span>
</div>
<?php
} // Close euqs while()
} // close $eqs while()
} // close $ss while()
} // close if()
Here are some sample records for each table.
quiz table:
(uid, category_uid, name, sample, difficulty, date_created, date_updated)
(1, 1, 'Business and Industry', '\0\0', 'Newcomer', '2012-03-15 13:42:30', '2012-03-15 13:42:30'),
(2, 2, 'History', '\0\0', 'Newcomer', '2012-03-15 13:42:30', '2012-03-15 13:42:30'),
(3, 3, 'Sports', '\0\0', 'Newcomer', '2012-03-15 13:42:50', '2012-03-15 13:42:50'),
(4, 4, 'Arts/Entertainment', '\0\0', 'Newcomer', '2012-03-15 13:42:50', '2012-03-15 13:42:50'),
(5, 5, 'Music', '\0\0', 'Newcomer', '2012-03-15 13:43:11', '2012-03-15 13:43:11'),
(6, 6, 'Geography', '\0\0', 'Newcomer', '2012-03-15 13:43:11', '2012-03-15 13:43:11');
scores table:
(uid, unique_uid, question_uid, quiz_uid, user_uid, answer, correct, points, time, date_created)
(81, '1111334693628', 4, 1, 11, 'Paul''s Valley', '0\0', 0, 2.8, '2012-04-17 13:15:40'),
(82, '1111334693628', 6, 1, 11, 'Bartlesville', '1\0', 9, 2.4, '2012-04-17 13:15:44'),
(83, '1111334693628', 3, 1, 11, 'Shawnee', '1\0', 8, 5.9, '2012-04-17 13:15:51'),
(84, '1111334693628', 40, 1, 11, 'Cimarron Turnpike', '0\0', 0, 4.4, '2012-04-17 13:15:57'),
(85, '1111334693628', 1, 1, 11, 'tow package for trucks', '1\0', 9, 3.9, '2012-04-17 13:16:03'),
(86, '1111334693628', 36, 1, 11, 'aviation', '1\0', 6, 9.0, '2012-04-17 13:16:13'),
(87, '1111334693628', 37, 1, 11, 'Altus', '0\0', 0, 3.0, '2012-04-17 13:16:18'),
(88, '1111334693628', 2, 1, 11, 'Bama Pies', '1\0', 7, 6.1, '2012-04-17 13:16:25'),
(89, '1111334693628', 5, 1, 11, 'Gordon Cooper', '0\0', 0, 2.2, '2012-04-17 13:16:29'),
(90, '1111334693628', 38, 1, 11, 'Bartlesville', '1\0', 9, 2.7, '2012-04-17 13:16:33'),
(91, '1131334773558', 13, 3, 11, 'Jim Thorpe', '1\0', 10, 1.5, '2012-04-18 11:26:09'),
(92, '1131334773558', 49, 3, 11, 'Henry Iba', '1\0', 10, 1.8, '2012-04-18 11:26:12'),
(93, '1131334773558', 17, 3, 11, 'Kelli Litsch', '1\0', 10, 1.9, '2012-04-18 11:26:15'),
(94, '1131334773558', 14, 3, 11, 'Bud Wilkinson', '0\0', 0, 4.4, '2012-04-18 11:26:21'),
(95, '1131334773558', 48, 3, 11, 'Charlie Coe', '1\0', 10, 1.7, '2012-04-18 11:26:25'),
(96, '1131334773558', 50, 3, 11, 'Jim Tatum', '1\0', 8, 4.3, '2012-04-18 11:26:31'),
(97, '1131334773558', 47, 3, 11, 'Bobby Murcer', '0\0', 0, 2.4, '2012-04-18 11:26:34'),
(98, '1131334773558', 15, 3, 11, 'Myron Roderick', '1\0', 9, 3.1, '2012-04-18 11:26:39'),
(99, '1131334773558', 46, 3, 11, 'Tommy McDonald', '1\0', 9, 3.6, '2012-04-18 11:26:44'),
(100, '1131334773558', 16, 3, 11, 'five', '0\0', 0, 2.0, '2012-04-18 11:26:48'),
(101, '1131334773620', 15, 3, 11, 'Myron Roderick', '1\0', 9, 2.4, '2012-04-18 11:27:16'),
(102, '1131334773620', 13, 3, 11, 'Jim Thorpe', '1\0', 10, 1.1, '2012-04-18 11:27:18'),
(103, '1131334773620', 49, 3, 11, 'Henry Iba', '1\0', 10, 1.3, '2012-04-18 11:27:21'),
(104, '1131334773620', 16, 3, 11, 'seven', '1\0', 10, 1.8, '2012-04-18 11:27:25'),
(105, '1131334773620', 46, 3, 11, 'Tommy McDonald', '1\0', 10, 1.4, '2012-04-18 11:27:28'),
(106, '1131334773620', 47, 3, 11, 'Darrell Porter', '1\0', 10, 1.8, '2012-04-18 11:27:31'),
(107, '1131334773620', 50, 3, 11, 'Jim Tatum', '1\0', 9, 2.2, '2012-04-18 11:27:35'),
(108, '1131334773620', 14, 3, 11, 'Benny Owen', '1\0', 9, 2.7, '2012-04-18 11:27:39'),
(109, '1131334773620', 17, 3, 11, 'Kelli Litsch', '1\0', 10, 1.8, '2012-04-18 11:27:42'),
(110, '1131334773620', 48, 3, 11, 'Charlie Coe', '1\0', 10, 1.9, '2012-04-18 11:27:46');
What about using one MySQL query like
SELECT * FROM quiz LEFT JOIN scores ON quiz.uid = scores.quiz_uid
And then just loop thru it?
I was able to come up with a solution by using GROUP. For anyone that might be interested, here's what I did:
// Query retrieves each unique play of each quiz the player has scored
$ss = sqlQuery("SELECT quiz.name, quiz.difficulty, scores.unique_uid, scores.quiz_uid, scores.date_created, SUM(scores.time) AS times, SUM(scores.points) AS points, SUM(scores.correct) AS ttlcorrect FROM scores, quiz WHERE scores.user_uid = {$_SESSION['uid']} AND quiz.uid = scores.quiz_uid GROUP BY scores.unique_uid ORDER BY quiz_uid, scores.date_created DESC");
$last_quiz = 0;
if ( mysql_num_rows( $ss ) > 0 ) {
while ( $row = mysql_fetch_assoc( $ss ) ){
/* Step through each play of each quiz */
if ( $row['quiz_uid'] != $last_quiz ) {
// Output Details
$last_quiz = $row['quiz_uid'];
?>
<h2><?php echo ( $row['name'] ); ?> <span><small>Difficulty: <?php echo( $row['difficulty'] ); ?></small></span></h2>
<?php
}
// Output Scores
?>
<div class="row">
<span class="score"><?php echo( number_format( $row['points'], 0) ); ?> points</span>
<span class="time"><?php echo( number_format( $row['times'], 0) ); ?> seconds</span>
<span class="correct"><?php echo( number_format( $row['ttlcorrect'], 0) ); ?> / 10 correct</span>
<span class="date">Played <?php echo( date( 'F j, Y', strtotime($row['date_created']) ) ); ?></span>
</div>
<?php
} // $ss while()
} // close if
?>

I want to get all auto_incremented_id of a table in comma seperated format with one query is it possible?

Table structure is like given below
id Name
1 Mukesh
2 Shiwam
3 deepak
4 ------
I want in resultant Like 1,2,3,4 it is possible?
Use GROUP_CONCAT():
SELECT GROUP_CONCAT(id)
FROM table
It's as simple as it gets. Cularis' answer specifies using the , separator, but it's not actually necessary, unless you want spaces between the commas:
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49
Which you didn't specify in your OP.
SELECT GROUP_CONCAT(id) FROM yTable
GROUP_CONCAT
Wth do you need this?

Categories