question table
========================
question_id
1
2
3
4
user_answer table
========================
user_id question_id
33 2
44 4
33 1
44 3
This code will return the question id (2 and 1)
what I want is to retrieve the other question id from table question so I want
the result to be (3 and 4)
$fadi = mysql_query("SELECT * FROM question
LEFT OUTER JOIN user_answer
ON user_answer.question_id = question.question_id
WHERE user_answer.user_id = 33");
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array($fadi))
{ Print "<tr>";
Print "<th>question </th> <td>".$info['question_id'] . "</td></tr>";
} Print "</table>"; }
I believe what you are looking for are the words IS NULL:
$fadi = mysql_query("SELECT * FROM question
LEFT OUTER JOIN user_answer
ON user_answer.question_id = question.question_id
AND user_answer.user_id = 33
WHERE user_answer.question_id IS NULL");
You can go one step further by only retrieving the question ID from the question table:
$fadi = mysql_query("SELECT question.question_id FROM question
LEFT OUTER JOIN user_answer
ON user_answer.question_id = question.question_id
AND user_answer.user_id = 33
WHERE user_answer.question_id IS NULL");
Edit: Improved version.
$fadi = mysql_query("SELECT * FROM question WHERE question.question_id NOT IN (SELECT user_answer.question_id FROM user_answer WHERE user_answer.user_id = 33)");
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array($fadi))
{ Print "<tr>";
Print "<th>question </th> <td>".$info['question_id'] . "</td></tr>";
} Print "</table>"; }
MySQLi version:
$link = mysqli_connect($hostname, $username, $password, $database);
if (!$link){
echo('Unable to connect to database');
}
else{
$fadi = mysqli_query("SELECT * FROM question WHERE question.question_id NOT IN (SELECT user_answer.question_id FROM user_answer WHERE user_answer.user_id = 33)", $link);
Print "<table border cellpadding=3>";
while($info = mysqli_fetch_array($fadi,MYSQL_BOTH))
{ Print "<tr>";
Print "<th>question </th> <td>".$info['question_id'] . "</td></tr>";
} Print "</table>"; }
}
mysqli_close($link);
See in action: http://www.sqlfiddle.com/#!2/27b6f/21
What could be an answer is to take the same mysql_query you used
then from mysql_fetch_array retrieve the 'question_id' values and finally, if you have the count of the questions (if not you can use mysql count), to use php function array_diff() to retrieve (from the incremental sorted array of integer values of question_id or from the array of question_id values from 'question' table) exactly what you want
Related
I'm displaying customer forms id row by row and that works fine. But I would also like to display the score of each form in the same row. How can i get the form_id from query 1 to work in query 2? I need the form_id to be able to sum the score for query 2.
I have added $WHAT-TO-PUT-HERE? in the code where I'm lost, what would be the correct way to solve this?
// get user_if from url
$user_id = $_GET['userid'];
// fetch form_id for form list
$query1 = "SELECT DISTINCT form_id FROM answers where user_id=$user_id";
$result1 = #mysqli_query($con, $query1);
while($row1 = mysqli_fetch_array($result1)) {
// get score based on form_id from query 1?
$result2 = mysqli_query($con,"SELECT SUM(answer_value) AS Poang FROM answers q JOIN questions a ON q.question_id = a.question_id WHERE form_id = $WHAT-TO-PUT-HERE? AND question_sort = 1");
while($row2 = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td><a href='currentform?formid=" . $row1['form_id'] . "</a></td>";
echo "<td>".$row2['Poang'] ."</td></tr></table>";
}}
You need to use row1['form_id'] instead of $WHAT-TO-PUT-HERE.
In your
while($row1 = mysqli_fetch_array($result1)) {
The current result of your Select will be saved into $row1 as array, so you can access these values like a normal array.
If you give the select an alias the key of the array will have the name of the alias, else it will have the column name from the database.
You also need to concate the strings like this
$result2 = mysqli_query($con,"SELECT SUM(answer_value) AS Poang FROM answers q JOIN questions a ON q.question_id = a.question_id WHERE form_id = ".$row1['form_id']." AND question_sort = 1");
replace $WHAT-TO-PUT-HERE by $row1['form_id']
like
$result2 = mysqli_query($con, "SELECT SUM(answer_value) AS Poang FROM answers q
JOIN questions a ON
q.question_id = a.question_id
WHERE form_id = '".$row1['form_id']."' AND question_sort = 1");
This can be done with just one query:
SELECT
a.form_id,
SUM(answer_value) AS Poang
FROM
answers a
JOIN
questions q
WHERE
a.user_id = $user_id
AND
q.question_sort = 1
GROUP BY
a.form_id
Your $row1 variable contains an associative array including your database row.
There for use $row1['form_id'] or $row1[0] (column 0) to access the value
I have 3 mysql tables. I want to display leaderboard for team in descending order of d_money for particular day (like day 1, day 2, day 3.)
user(u_id(p),name)
team(t_id(p),u_id(f),t_name,t_money,days_money) and
history(t_id(f),day,d_money).
First i collected all t_id's in $tid_arr. Then for each t_id, i wrote query to get t_name and its days money.(for particular day. Here - day 1). It displays the result. But i want the result sorted (descending order of d_money). But i couldn't find the soultion.
$query = $con->prepare("SELECT t_id FROM team");
$query->execute();
$tid_arr = $query->fetchAll();
echo "<table border='1'>";
foreach($tid_arr as $tid)
{
$que = $con->prepare("SELECT d_money, t_name FROM team, history WHERE history.t_id=$tid['t_id'] AND team.t_id=history.t_id` AND history.day='1'");
$que->execute();
while($info = $que->fetch(PDO::FETCH_NUM))
{
echo "<tr>";
echo "<td>".$info[0]."</td>";
echo "<td>".$info[1]."</td>";
echo "</tr>";
}
}
echo "</table>";
If you want to order by d_money then t_id, you can use the following query. The 1st query is unnecessary and should be removed.
SELECT d_money, t_name FROM team a
LEFT JOIN history b ON a.t_id=b.t_id
WHERE history.day='1'
ORDER BY d_money DESC, t_id DESC
p.s. user table is not used ?
Add a Join and Order By clause to your SQL statement to be
echo "<table border='1'>";
$que = $con->prepare("SELECT T.t_id, H.d_money, T.t_name
FROM team T INNER JOIN history H ON T.t_id=H.t_id
WHERE H.day='1'
ORDER BY d_money DESC");
$que->execute();
while ($info = $que->fetch(PDO::FETCH_NUM)) {
echo "<tr>";
echo "<td>".$info[0]."</td>";
echo "<td>".$info[1]."</td>";
echo "</tr>";
}
echo "</table>";
I am using the following tables:
artists
related_artists
The idea is when I enter an artist's page with an artist_id I can use that id to load related artists. The following code works, but how do I put it in a single query? I can't figure it out.
To connect related_artists with artists I created the following code:
$sql = "SELECT related_artist_id FROM related_artists WHERE artist_id = 1";
$res = mysqli_query($db, $sql);
if (!$res) {
echo "Er is een fout opgetreden.";
exit;
} else {
while ($row = mysqli_fetch_array($res)) {
$query = 'SELECT * FROM artists WHERE artist_id = '.$row["related_artist_id"];
print_r($query."<br />\n");
$result = mysqli_query($db, $query);
if ($result) {
while ($test = mysqli_fetch_array($result)) {
echo $test["lastName"]."<br />\n";
}
} else {
echo "It doesn't work";
exit;
}
}
}
You can just try :
select *
from artists
where artist_id in (
select related_artist_id
from related_artists
WHERE artist_id = 1
);
You can use a LEFT JOIN, try this:
SELECT b.*
FROM related_artist a
LEFT JOIN artists b
USING(artist_id)
WHERE a.artist_id = 1
Should return * from artists, where I aliased artists as b and related_artist as a.
Didn't test, does it work for you / return the expected result?
SELECT * FROM artists where artists.arist_id = 1
INNER JOIN related_artist ON related_artist.artist_id = artists.artist_id
This provides a join on the artist_id columns of both tables, having artist_id = 1. I'm not sure if you need an Inner or a Left join
I can't figure out join..
need to select all rows from one table where a column equals something in another table..
like this:
SELECT ALL FROM someTable
WHERE COLUMN someColumn = '123' (IN A DIFFERENT TABLE)
something like that..
and the IDs need to match of course..
Just use an INNER JOIN:
SELECT *
FROM SomeTable S
JOIN SomeOtherTable S2
ON S.SomeKey = S2.SomeKey
WHERE S.SomeColumn = '123'
A Visual Explanation of SQL Joins
I wasn't completely clear of your question, so you may not need the WHERE clause if that represented your JOIN criteria.
A bit error in my question, let me clarify
tblplace
id place
1 London
2 Paris
3 New York
tbltraveller
tr_id tr_name id
1 John 3
2 Jackson 2
3 Susanna 1
4 Jimmy 3
5 Lucy 2
The problem is missing some data for New York:
Paris Jackson Lucy
New York
My code(guess there is an error in the while loop):
<?php
require_once("connMysql1.php");
$qry = "SELECT * FROM tblPlace WHERE tblPlace.id > 1 ORDER BY tblPlace.id";
$result = mysqli_query($db_link, $qry);
$qry1 = "SELECT tblPlace.id, tblPlace.Place, tbltraveller.tr_id,
tbltraveller.tr_name, tbltraveller.id FROM tblPlace INNER JOIN tbltraveller ON
tblPlace.id = tbltraveller.id WHERE tblPlace.id > 1 ORDER BY tbltraveller.id";
$result1 = mysqli_query($db_link, $qry1);
$no_of_row1 = mysqli_num_rows($result1);
$no_of_row = mysqli_num_rows($result);
echo "<table border=1 cellpadding=10>";
if ($no_of_row >0){
while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>";
echo $row['place']."<br>";
echo "</td>"." ";
echo "<td>";
while ($row1 = mysqli_fetch_assoc($result1)){
if($row1['id'] == $row['id']){
echo $row1['tr_name']." ";
}
}
echo "</td>";
"</tr>";
}
}
echo "</table>";
?>
`
My question is why it don't show $aantalkeermenu. but it show's the other variable's from the query can anyone help me with that?
This is my query:
$sql = "SELECT res.reserveringsnr, res.datum, menr.reserveringsnr, menr.menunr, SUM(menr.aantalkeermenu), men.menunr, men.menunaam, men.voorgerecht, men.hoofdgerecht, men.nagerecht FROM reserveringen AS res
INNER JOIN menus_regel as menr
ON res.reserveringsnr = menr.reserveringsnr
INNER JOIN menus AS men
ON menr.menunr = men.menunr
WHERE res.datum = '".$invoerdatum."'
GROUP BY menr.menunr
";
Here is show it on my webpage:
$result = mysql_query($sql);
echo "<table>";
echo "<th>Aantal</th> <th> Menunaam </th><th> Voorgerecht </th> <th> Hoofdgerecht </th> <th> Nagerecht </th>";
while($row = mysql_fetch_assoc($result)) {
$datum = $row['datum'];
$aantalkeermenu = $row['aantalkeermenu'];
$menunr = $row['menunr'];
$menunaam = $row['menunaam'];
$voorgerecht = $row['voorgerecht'];
$hoofdgerecht = $row['hoofdgerecht'];
$nagerecht = $row['nagerecht'];
$aantalkeermenu = $row['aantalkeermenu'];
// open tr
echo "<tr id='$menunr' class='edit_tr'>";
echo "<td><span>$menunr</span></td>";
echo "<td><span>$aantalkeermenu</span></td>";
echo "<td><span>$menunaam</span></td>";
echo "<td><span>$voorgerecht</span></td>";
echo "<td><span>$hoofdgerecht</span></td>";
echo "<td><span>$nagerecht</span></td>";
echo "</tr>";
// close tr
}
echo "</table>";
When you
select ... SUM(menr.aantalkeermenu)
the resulting column won't be called aantalkeermenu. You need to explicitely name the column, like this
select ... SUM(menr.aantalkeermenu) as aantalkeermenu
and then it will appear in under that name in the resulting row.
You need to alias the SUM(menr.aantalkeermenu) other wise I believe it is returned as SUM(menr.aantalkeermenu)
The following SQL should help.
$sql = "
SELECT res.reserveringsnr, res.datum, menr.reserveringsnr, menr.menunr,
SUM(menr.aantalkeermenu) AS aantalkeermenu, men.menunr, men.menunaam, men.voorgerecht,
men.hoofdgerecht, men.nagerecht
FROM reserveringen AS res
INNER JOIN menus_regel as menr
ON res.reserveringsnr = menr.reserveringsnr
INNER JOIN menus AS men
ON menr.menunr = men.menunr
WHERE res.datum = '".$invoerdatum."'
GROUP BY menr.menunr";