im trying to combine two tables together, but whenever i run the program, this happens.
As you can see i've echoed out the sql statement. Here are my codes.
$queryc1 = "select sum(repeater),sum(membersigned) from sales UNION ALL select count(*) from approach;"; //DO INNERJOIN PRACTISE
$resultc1 = mysqli_query($dbconn,$queryc1);
echo "<table>
<tr>
<th>Repeater</th>
<th>Members</th>
<th>Approach</th>
</tr>";
while($row = mysqli_fetch_array($resultc1)) {
echo "<tr>";
echo "<td>" . $row['sum(repeater)'] . "</td>";
echo "<td>" . $row['sum(membersigned)'] . "</td>";
echo "<td>" . $row['count(*)'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo $queryc1;
I'd like to show count(*) as in the photo, as a third column to the genrated table.
You don't need UNION select here, instead you can use subquery:
select sum(repeater) as repeater_sum,
sum(membersigned) as membersigned_sum,
(select count(*) from approach) as approach_count
from sales;
and in PHP you use $row['repeater_sum'].. etc
When you union two or more queries together each query should have the same columns of data with same data type for example :
SELECT SUM(repeater),SUM(membersigned) FROM sales
UNION
SELECT Text1,Text2 FROM approach
Replace your query with this format
Related
I have 2 tables called 0_vrs_american and 0_vrs_europe, and I need to display the rows of these tables in a single html table. So I wrote this code:
<?php
$con=mysqli_connect("localhost","aaa","bbb","my_mk7vrlist");
$result = mysqli_query($con,"SELECT 0_vrs_american.*, 0_vrs_europe.* FROM 0_vrs_american, 0_vrs_europe ORDER BY `vrs` DESC LIMIT 0 , 200");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $x . "</td>";
echo "<td>" . $row['playername'] . "</td>";
echo "<td>" . $row['contactable'] . "</td>";
echo "<td>" . $row['vrs'] . "</td>";
echo "</tr>";
$x = $x+1;
}
mysqli_close($con);
?>
I am pretty new with MySQL and so I googled about this topic and I found a syntax like the one you can see above. By the way I have no results in my page because the table is empty.
So: I must display in a HTML table the content of both sql tables, and the rows must be sorted according with the vrs (number that goes from 50000 to 99000). How could I solve my problem?
An alternative to the above is to select all the rows from those two tables as an inner select, and then order by vrs in the returned result set.
SELECT *
FROM
(
SELECT * FROM 0_vrs_american
UNION
SELECT * FROM 0_vrs_europe
) AS a
ORDER BY vrs
at first U need to define $x before the loop
then use the query like this
$result = mysqli_query($con,"SELECT 0_vrs_american.*, 0_vrs_europe.* FROM 0_vrs_american, 0_vrs_europe ORDER BY `vrs` DESC LIMIT 0 , 200") or die (__LINE__." ".mysqli_error($con));
so you will see the error and the line of the query
i just add or die (__LINE__." ".mysqli_error($con))
Separate the 2 queries and their results
merge both results into an array
sort the array as needed
output the array (generate table rows)
Have 3 different mysql tables, my script queries the DB using JOINS to output the most recent results from each of the 3 tables. All works like it is supposed to but the way mysql databases allow for JOINS, the ORDER BY will order the results as you list the tables, resulting in the finished output not being ordered by date correctly, since the results are from 3 different MYSQL tables.
The results instead are ordered by date correctly only next to the other results form the same table. See the script and results below: (I am limit 1 for the output for simplicity)
$username = $_SESSION['username'];
$result = $mysqli->query("
SELECT
accounts.account_name,
cases.case_subject,
tasks.task_title,
accounts.accounts_date_last_edited,
cases.cases_date_last_edited,
tasks.tasks_date_last_edited
FROM
accounts,
cases,
tasks
WHERE
accounts.username = cases.username
AND cases.username = tasks.username
ORDER BY
accounts.accounts_date_last_edited DESC,
cases.cases_date_last_edited DESC,
tasks.tasks_date_last_edited DESC
LIMIT 1
");
if (!$result) { echo "Error Result5: " . $mysqli->error; } else {
echo "<table>";
echo "<tr><th>Item</th><th>Date</th></tr>";
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['account_name'] . "</td><br>";
echo "<td>" . $row['accounts_date_last_edited'] . "</td><br>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['case_subject'] . "</td><br>";
echo "<td>" . $row['cases_date_last_edited'] . "</td><br>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['task_title'] . "</td><br>";
echo "<td>" . $row['tasks_date_last_edited'] . "</td><br>";
echo "</tr>";
}
echo "</table>";
The Results are below: they will list 1 item from each table listed above. Again I am trying to sort by the date_last_edited regardless of which table the values are from.
Item Date
Shorley Homes 2013-11-02 04:02:34
Techical Testing Case Open 2013-11-03 07:17:36
Icons 2013-11-03 07:28:02
If I read correctly (and I'm not sure of that) your
...sort by the date_last_edited regardless of which table the values are from...
then you might want to use UNION instead of JOIN
SELECT account_name item, accounts_date_last_edited date
FROM accounts
UNION ALL
SELECT case_subject, cases_date_last_edited
FROM cases
UNION ALL
SELECT task_title, tasks_date_last_edited
FROM tasks
ORDER BY date DESC
Here is SQLFiddle demo
I am trying to display data from multiple rows in sql in a single table box.
in the format of:
"MovieTitle" "movieReleaseYear" "directorName"
The Matrix 1999 Andy Wachowski, Lana Wachowski
where Andy Wachowski and Lana Wachowski comes from different rows but are gathered with group_concat
I have no problem getting them in phpmyadmin but i don't know how to display it in php.
I have this:
$sql="SELECT 725G54_5_movies.MovieTitle, 725G54_5_movies.movieProductionYear, GROUP_CONCAT( 725G54_5_director.directorName )
FROM 725G54_5_movies
JOIN 725G54_5_directed ON 725G54_5_movies.MovieID = 725G54_5_directed.movieID
JOIN 725G54_5_director ON 725G54_5_directed.directorID = 725G54_5_director.directorID
GROUP BY 725G54_5_movies.MovieTitle
ORDER BY $order ASC";
$result=mysql_query($sql);
//Presentation av kontakterna via while-sats till ett formulär
while($rows=mysql_fetch_array($result)){
echo "<tr>
<td>"; echo $rows['MovieTitle']; echo "</td>
<td>"; echo $rows['movieProductionYear']; echo "</td>
<td>";
while($director=mysql_fetch_array($rows['directorName'])){ echo $director; };
echo"</td>
</tr>";
Group Concat has already concatenate the name of the directors, so you don't need to iterate on $rows['directorName'] .This part in your code id wrong because $rows is a row from the query result and $rows['director'] is a value from this row, you can't iterate on a value.
$sql = "SELECT GROUP_CONCAT( 725G54_5_director.directorName ) AS directorNames ..."
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $rows['MovieTitle'] . "</td>";
echo "<td>" . $rows['movieProductionYear'] . "</td>"
echo "<td>" . $rows['directorNames'] . "</td>"
echo"</tr>";
}
You use GROUP BY 725G54_5_movies.MovieTitle so directorNames field will be the result of all directorName concatenated for this Movie.
The crucial bit of information is that you need to alias your GROUP_CONCAT so that you can access this in the array. I would also use a Heredoc when outputting blocks of html for readability:
$sql=
"SELECT
725G54_5_movies.MovieTitle,
725G54_5_movies.movieProductionYear,
GROUP_CONCAT(725G54_5_director.directorName) directorNames
FROM
725G54_5_movies
JOIN
725G54_5_directed
ON 725G54_5_movies.MovieID = 725G54_5_directed.movieID
JOIN
725G54_5_director
ON 725G54_5_directed.directorID = 725G54_5_director.directorID
GROUP BY
725G54_5_movies.MovieTitle
ORDER BY
$order ASC";
$result=mysql_query($sql);
//Presentation av kontakterna via while-sats till ett formulär
echo "<table>";
while($rows=mysql_fetch_array($result)){
echo <<<HTML
<tr>
<td>{$rows['MovieTitle']}</td>
<td>{$rows['movieProductionYear']}</td>
<td>{$rows['directorNames']}</td>
</tr>
HTML;
}
echo "</table>";
I have 2 queries.
I am applying the first select query, and getting the results via the while loop using my_fetch_array, however, inside that while, I am using another select query, but at that point I am getting an error.
Can't I use another select inside the while ?
$i = 1;
$result1 = mysql_query("SELECT x, count(*) FROM table1 group by x ORDER BY count(*) desc");
while ($rowRes1= mysql_fetch_array($result1 ))
{
echo "<tr>";
echo "<td ALIGN=CENTER style=width:5%>" . $i . "</td>";
echo "<td ALIGN=CENTER style=width:40%>" . $rowRes1['0'] . "</td>";
echo "<td ALIGN=CENTER style=width:10%>" . $rowRes1['1'] . "</td>";
$result2 = mysql_query("SELECT x FROM data where x = $rowRes1['0'] ORDER BY y ASC LIMIT 1")
while ($rowRes2= mysql_fetch_array($result2 ))
{
echo "<td ALIGN=CENTER style=width:10%>" . $rowRes2['0'] . "</td>";
}
$i++;
}
PHP cannot interpret strings containing array accessors like $rowRes1['1'] within double quoted strings.
store the data in another variable like $rowRes1Col = $rowRes1['0']; and use that inside the string.
I have a select on a database, like this:
$result = mysql_query("
SELECT dat_eb_registrants.id, dat_eb_registrants.first_name,
dat_eb_registrants.last_name, dat_eb_registrants.email, dat_eb_registrants.comment,
dat_eb_registrants.amount, dat_eb_registrants.published,
dat_eb_registrants.transaction_id, dat_eb_registrants.register_date,
dat_eb_field_values.field_value
FROM dat_eb_registrants LEFT JOIN dat_eb_field_values
ON dat_eb_registrants.id=dat_eb_field_values.registrant_id
WHERE dat_eb_field_values.field_id='53' AND `event_id` >= 20 AND `event_id` <= 25
ORDER BY $sort $ascdsc
");
and it gets diplayed in a html table like so:
echo "
ID
First name
Last name
Email
Comment
Value1
Value2
Value3
";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[9] . "</td>";
echo "<td>" . $row[?] . "</td>";
echo "<td>" . $row[?] . "</td>";
echo "</tr>";
echo "</table>";
}
Now, the first 4 values are being displayed correctly, just like the 5th one, but how about the 6th and the 7th one? they are not being called by MYSQL because dat_eb_field_values.field_value is only called one time, and is assigned the value WHERE dat_eb_field_values.field_id='53
How can i complete the table with the other values in the database?
Thanks in advance, Laurent
If I understand correctly, you are trying to load multiple values for each person. These values are in multiple rows in a separate table.
You'll need to use a GROUP BY clause. JOIN the registrants table with the values table just like you do now, and after the WHERE clause, GROUP BY the registrant's id. And of course, don't restrict the WHERE to a certain value's ID, because then you won't get any other values. Finally, you can use GROUP_CONCAT() to get all the value rows into a single result value, delimited by ,. You can change this to be delimited by </td><td> to make it easier to put into your table.
SELECT registrants.name, GROUP_CONCAT(values.value SEPARATOR '</td><td>')
FROM registrants
LEFT JOIN values USING (registrant_id)
WHERE ...
GROUP BY registrant_id
Voitek Zylinski answer is I think the best way to do this. but you could try a for each.
while ($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $row_item)
{
echo "<td>".$row_item."</td>"
}
echo "</tr>";
}
This would work no matter how many field