I've a page where I want to use 3 times INNER JOIN, Because log_items have a row called 'price'. When I do INNER join with log_mobs & log_mitem everything goes fine, He gets all information from $_GET['id'] combined with ['g'] but when I join with log_items he shows all rows from the table 'log_items'
I want to show on the page, the items the MOB have next to that I want to show the price.
But he shows all those information from 'log_items' I need to show items from 'log_mitem' but I need to get those prices from 'log_items'
if (isset($_GET['id']) && isset($_GET['g']))
{
$id = $db->real_escape_string(trim($_GET['id']));
$g = $db->real_escape_string(trim($_GET['g']));
$mobitem = $db->query("SELECT * FROM log_mobs INNER JOIN log_mitem INNER JOIN log_items ON log_mobs.name = log_mitem.mobname AND log_mobs.game = log_mitem.game WHERE log_mobs.name = '".$id."' AND log_mitem.game = '".$g."'") or die($db->error);
?>
I think your SQL request should look something like this:
select * from log_mobs
inner join log_mitem on log_mobs.name = log_mitem.mobname
inner join log_items on log_items.column = theOtherSideOfTheJointure.column
Related
I have a drop down menu form like so: https://ibb.co/eX5BhH
Each selected option will filter the query and return the query. So far, my "disability" and "veteran" option are querying correctly. I'm trying to get the "Type of Resource" category to work by using inner join. Here's the sql command:
SELECT shelter_name, shelter_address, disability, veteran
FROM shelter s
INNER JOIN shelter_type st ON s.shelter_id = st.shelter_id
INNER JOIN s_type stt on st.type_id = stt.type_id
WHERE type_name = "Charitable Organization";
This sql command works but it only query the resources under the "Charitable Organization" category. The user will be able to choose other category so instead of "Charitable Organization" I replace it with "$value_4":
SELECT shelter_name, shelter_address, disability, veteran
FROM shelter s
INNER JOIN shelter_type st ON s.shelter_id = st.shelter_id
INNER JOIN s_type stt on st.type_id = stt.type_id
WHERE type_name = "$value_4";
Here is my php code:
<?php
require'/Library/WebServer/Documents/Require/require.php';
$value_1 = $_POST["county"];
$value_2 = $_POST["disability"];
$value_3 = $_POST["veteran"];
$value_4 = $_POST["resources"];
$sql = "SELECT shelter_name, shelter_address, disability, veteran
FROM shelter s
INNER JOIN shelter_type st ON s.shelter_id = st.shelter_id
INNER JOIN s_type stt on st.type_id = stt.type_id
WHERE type_name = '$value_4'";
$result = mysqli_query($dbc, $sql) or die (mysql_error());
echo"<table border='1'>";
echo"<tr><td>Shelter</td><td>Address</td><td>Disability</td></tr>";
//iterate through the table
while ($row = mysqli_fetch_assoc($result)){
echo"<tr><td>{$row['shelter_name']}</td><td>{$row['shelter_address']}</td><td> {$row['disability']}</td><td>{$row['veteran']}</td></tr>";
}
echo"</table>"; //close table
echo"success";
?>
However, when I use "$value_4", the table doesn't display. If I change it back to "Charitable Organization", the table is displayed again. What can I do to make my table display when I use "$value_4"?
I think to resolve this issue you should be using LEFT JOIN rather than INNER JOIN.
Inner Joins expect data from the left table and right table to match. If it only exists in the left table but not the right then it won't show the records.
By using Left Join you are saying you want everything from the left table and then if there is information from the right table (i.e. the one you're joining on) then bring that too.
It might be worth reading this tutorial on joins to help you get more experience.
Hi Guys I'm having a Problem on how i could make a if else statement on SQL,
It only displays the value of rent.rent_status = 1 on a Modal, but if rent.rent_status has no value the modal wont display, how can i make it display?
Here is my code for your reference;
$sql = "SELECT * FROM stall
LEFT JOIN tenant ON tenant.stall_id = stall.stall_id
LEFT JOIN rent ON rent.tenant_id = tenant.tenant_id
WHERE rent.rent_status = 1 AND stall.stall_id = 1";
You are using LEFT JOIN and then undoing the outer join in the WHERE clause. You need to move the conditions to the appropriate ON clauses:
SELECT *
FROM stall s LEFT JOIN
tenant t
ON t.stall_id = s.stall_id LEFT JOIN
rent r
ON r.tenant_id = t.tenant_id AND r.rent_status = 1
WHERE s.stall_id = 1;
A LEFT JOIN keeps all rows in the first table along with matching rows in the second table. If there is no match, then the values are NULL for the columns in the second table. Your WHERE condition fails on NULL conditions.
Maybe try Wrapping your output with code similar to this
while (mysqli_stmt_fetch($stmt)) {
if(mysqli_stmt_num_rows($stmt) > 0){'output goes here'}
}
I'm not sure how you're using your php , this is just to give you an idea to grow on.
You don't need to use if..else just use or operator....
SELECT * FROM stall
LEFT JOIN tenant ON tenant.stall_id = stall.stall_id
LEFT JOIN rent ON rent.tenant_id = tenant.tenant_id
WHERE ((rent.rent_status = 1) OR (rent.rent_status IS NULL)) AND stall.stall_id = 1"
Hello I would like to know how I can realizes query selection inside echo of already processed query.
This is my firs Mysql Query
SELECT * FROM cursos_modulos
foreach($result as $row)
{
$id = $row['id'];
echo"
Here where the echo goes I have to make the other query which is:
SELECT COUNT(users.userID)
FROM users
INNER JOIN subscriptions
ON users.userID = subscriptions.user_id
WHERE subscriptions.curso_id = $id
and at the end to put the result of this query
foreach($result as $rowc)
.$rowc[0]."};
Any help how I can achive this goal will be very welcome. Question is simple. First Select Selects the Cours with it's unique ID. which ID have to be used in the second, third and else... queries. Which queries are like the second one. So First Select Course and then Select different parameters from this course based on this ID. at the end dump results of each of the selections with different indications"
Do it all in one query:
SELECT c.*, count(s.curso_id) as count
FROM cursos_modulos AS c
LEFT JOIN subscriptions AS s ON s.curso_id = c.id
LEFT JOIN users AS u ON u.userID = s.user_id
The LEFT JOIN is needed to get 0 for the count if there are no matching rows in subscriptions.
To include a second count of approved subscriptions:
SELECT c.*, count(s.curso_id) as count, SUM(IF(s.approved = 'approved', 1, 0)) AS count_approved
FROM cursos_modulos AS c
LEFT JOIN subscriptions AS s ON s.curso_id = c.id
LEFT JOIN users AS u ON u.userID = s.user_id
In database the users have a row called weapon_id which determines what weapon he uses from another table with weapons.
Is there a better way to get that info like join table or something?
$user_get = mysqli_query($db, "SELECT * FROM members WHERE id = '".$_SESSION['sess_id']."'");
$user = mysqli_fetch_assoc($user_get);
$weapon_get = mysqli_query($db, "SELECT * FROM weapons WHERE weapon_id = '".$user['weapon_id']."'");
$weapon = mysqli_fetch_assoc($weapon_get);
use a join like this
$user_get = mysqli_query($db, "SELECT * FROM members m LEFT JOIN weapons w ON(w.weapon_id=m.weapon_id) WHERE m.id = '".$_SESSION['sess_id']."'");
$user = mysqli_fetch_assoc($user_get);
change the join type as per your requirement. the above query for only example
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables
LEFT JOIN: Return all rows from the left table, and the matched rows
from the right table
RIGHT JOIN: Return all rows from the right table, and the matched
rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
more about join click here
AND also check this http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
SELECT * FROM `weapon` as w
JOIN `user` as u
on w.weapon_id = u.weapon_id
and u.id = :session_id
How about this ?
I have 3 tables:
-Sales
-Items_cstm
-Items
Sales and Items_cstm contains the data I have to get with the query and Items the ability of the item "Deleted (1 or 0)" (and dome more info I don't need).
The Items_cstm's id is = Items's id.
I have to list the Sales of the Items which aren't deleted (0).
I've tried somehow with inner join but it didn't work and I don't really know what am I doing wrong:
SELECT Items_cstm.quantity
FROM Items_cstm, Sales
WHERE '".$_POST['name']."' = Sales.name
AND
INNER JOIN Items ON Items_cstm.id = Items.id
WHERE Items.deleted = 0;
You can join your tables like this (but you did not show the link between Items_cstm and sales - you have to modify that)
SELECT ic.quantity
FROM Items_cstm ic
INNER JOIN Sales s on s.id = ic.id
INNER JOIN Items i ON ic.id = i.id
WHERE i.deleted = 0
AND s.name = '".$escapedName."'
Also always escape your user input.
Try something like this:
SELECT Items_cstm.quantity
FROM Items_cstm
INNER JOIN Sales ON sales.JOINCOLUMN = Items_cstm.JOINCOLUMN
INNER JOIN Items ON Items_cstm.id = Items.id
WHERE Sales.name = '".$_POST['name']."'
WHERE Items.deleted = 0;
Replace JOINCOLUMN above with the columns that let you match records between these two tables
Something like this should work:
SELECT Items_cstm.quantity
FROM Items_cstm
INNER JOIN Items ON Items_cstm.id = Items.id
INNER JOIN Sales ON Items_cstm.id = Sales.item_id // ??? check this one for column names
WHERE '$name' = Sales.name AND Items.deleted = 0;
Beware of SQL injection.