Join Statement for Multiple tables - php

I have a problem with displaying records once despite using inner join.
I have posted a question which have the code here
SELECT transaction_information . *, customer_information . * , property_information . *, borrowers . *, lenders . *, listing_agents . *, payoff . *, sellers . *, selling_agents .*
FROM transaction_information
JOIN customer_information ON transaction_information.entry_no = customer_information.entry_no
JOIN property_information ON transaction_information.entry_no = property_information.entry_no
JOIN borrowers ON transaction_information.entry_no = borrowers.entry_no
JOIN lenders ON transaction_information.entry_no = lenders.entry_no
JOIN listing_agents ON transaction_information.entry_no = listing_agents.entry_no
JOIN payoff ON transaction_information.entry_no = payoff.entry_no
JOIN sellers ON transaction_information.entry_no = sellers.entry_no
JOIN selling_agents ON transaction_information.entry_no = selling_agents.entry_no
";
I have a form at http://208.109.186.112/kushalmn/demo1/. It has two borrower sections and I am using borrowers in one single table. Also, Each section is stored in its own table. For example, Sellers have own table, lenders have own, customer information have own and so forth. If I fill two borrower entries and say customer info section only, I get multiple rows exceeding 100 maybe.
The view page for the form can be viewed here http://208.109.186.112/kushalmn/demo1/view.php.
It seems a problem with Joins . Common Column being Entry no which I am incrementing at each submit.

Related

how to join 2 tables in SQL

I need to join my variants table to my products table so that I am able to get the product_id from both tables, how can I put this into my existing query..?
I've tried to add it not working.
$query = query("
SELECT *
FROM products
FULL OUTER
JOIN variants WHERE product_id=" . escape_string($_GET['add']). " ");
It sounds like you are looking for an inner join, not a full outer join. You'll also want to specify the column you are joining both tables on:
$query = query("
SELECT *
FROM products
JOIN variants ON variants.product_id = products.product_id
WHERE products.product_id =" . escape_string($_GET['add']). " ");
*Note: I'm assuming product_id is a column on both the variants and products table. If, for example, the product_id of products is just id, you'll want to change the join condition to:
ON variants.product_id = products.id

How to avoid the looping data in Phpmyadmin after selecting a three different tables in database

When I selected data with different rows from three different tables, it suddenly duplicates, I should only fetch 4 rows but it became 25 rows, I've already used DISTINCT but it didn't work.
This is my SELECT statement:
SELECT seller.s_fname,
seller.trade_name,
order_details.od_id,
order_details.customer_id,
order_details.confirmation_status,
food_product.food_id,
food_product.food_name,
food_product.f_image
FROM seller
INNER JOIN order_details
ON seller.seller_id=order_details.seller_id
INNER JOIN food_product
ON seller.seller_id=food_product.seller_id
WHERE seller.seller_id=order_details.seller_id
AND customer_id = '" . $_SESSION['customer_id'] . "'
AND notif_status = 'uncleared'
AND od_status = 'ordered'
How can I avoid the duplicates?
Maybe two things you can try to avoid duplicates on your request :
In your WHERE clause you put again seller.seller_id=order_details.seller_id. It's already in your INNER JOIN CLAUSE above. Try to withdraw it.
Try to use GROUP BY clause with seller_ids.
You can try this edited SQL query :
SELECT seller.seller_id,
seller.s_fname,
seller.trade_name,
order_details.od_id,
order_details.customer_id,
order_details.confirmation_status,
food_product.food_id,
food_product.food_name,
food_product.f_image
FROM seller
INNER JOIN order_details
ON seller.seller_id=order_details.seller_id
INNER JOIN food_product
ON seller.seller_id=food_product.seller_id
WHERE customer_id = '" . $_SESSION['customer_id'] . "'
AND notif_status = 'uncleared'
AND od_status = 'ordered'
GROUP BY seller.seller_id

SQL Query JOIN syntax

I have the following tables and would like to query one more element from them.
categories table ->idcat(int), cat(varchar);
topics table ->idtopic(int), topic(varchar), idcat(int-fk), iduser(int-fk);
replies table ->idreply(int), reply(varchar) iduser(int-fk), idtopic(int-fk)
users table ->iduser(int), username(varchar).
My current query is;
$query = "SELECT t.topic, t.idtopic, u.username
FROM topics t
LEFT JOIN categories c ON t.idcat = c.idcat
LEFT JOIN users u ON t.iduser = u.iduser
WHERE c.idcat = '" . $idcat . "'";
Which presents 'Topic' and 'Username'. I'd like to show 'idReply' as well but don't know the proper JOIN syntax.
SELECT
t.topic,
t.idtopic,
u.username
FROM
topics t
LEFT JOIN
categories c ON t.idcat = c.idcat
LEFT JOIN
users u ON t.iduser = u.iduser
LEFT JOIN // new
replies r ON r.iduser = u.iduser AND r.idtopic = t.idtopic // new
WHERE c.idcat = '" . $idcat . "'";
This will generate a row for every reply to every topic in the specified category. Which could be a lot of records.
You may also want to experiment with the exact type of join on the replies table to get the result you want. LEFT JOIN is probably correct as you'll still get a result if there's no reply to a given topic. This may depend on your flavour of SQL.
OUTER JOIN & LEFT OUTER JOIN are possibilities.
Using INNER JOIN will ensure only topics with replies are returned.
You must have reply_id (foreign key) in your topics table. After that you can use this query.
$query = "SELECT t.topic, t.idtopic, u.username, r.id as reply_id
FROM topics t
LEFT JOIN categories c ON t.idcat = c.idcat
LEFT JOIN users u ON t.iduser = u.iduser
LEFT JOIN replies r ON t.reply_id = r.id
WHERE c.idcat = '" . $idcat . "'";

How to get results count from each entry ID - PHP/SQL?

here is a small sample of what im trying.. as its hardly to code it here I have uploaded an image..
http://i46.tinypic.com/5bxn5u.png
just want to display the total number of reservations for each room.. thanks
You'll want to join the tables together and use COUNT() with a GROUP BY:
SELECT
a.id, a.name, COUNT(*) AS num_reservations
FROM
availables a
LEFT JOIN reservations r
ON r.available_id = a.id
GROUP BY
a.id
This should give you a list of each room with a total number of reservations each has.
To put it into your existing query, you can use the following:
$coordinator = (isset($_GET['uidse']) && is_numeric($_GET['uidse'])) ? (int)$_GET['uidse'] : 0;
$result = mysql_query("SELECT *, COUNT(*) AS num_reservations FROM availables LEFT JOIN reservations ON availables.id=reservations.available_id WHERE coordinator='" . $coordinator . "' GROUP BY availables.id");
I placed $coordinator outside of the MySQL query and added very basic validation/sanitization to save you from a SQL-Injection headache in the future.
SELECT availables.name, COUNT(*) FROM availables
LEFT JOIN reservations ON reservations.available_id = availables.id
GROUP BY reservations.available_id

Optimizing Small MySQL Join Query

I have a list of venues in the Venues table, and a list of a cities/states in the Locations table. The venue is associated with an area code unique to my organization, referred to as a SOYID. The SOYID is made up of a geographical area - each row in the Locations table has a City, State, and the corresponding SOYID. Some Venues rows have a SOYID, others do not; for those that do not, I need to find the SOYID for the city and state listed. I only want to select those Venues in a specific SOYID.
This query works, however, it takes a few seconds to load; I don't think I am writing the query correctly. Currently Venues has approx 140 rows, Locations has 40,000.
$sql = "SELECT DISTINCT a.VenueID, a.Name, a.PhotoID, a.City, a.StateAbbr
FROM Venues AS a LEFT JOIN Locations AS c ON a.City = c.city
WHERE a.SOYID = '" . mysql_real_escape_string($SOYID) . "'
OR ((c.city = a.City) AND (c.state = a.StateAbbr) AND (c.SOYID = '" . mysql_real_escape_string($SOYID) . "'))
ORDER BY a.Name ASC";
Any time you reference a column from a LEFT JOINed table (c.state and c.SOYID in your specific case) in the WHERE clause, you force that join to behave like an INNER JOIN. Instead, make those tests part of the join condition:
"SELECT DISTINCT a.VenueID, a.Name, a.PhotoID, a.City, a.StateAbbr
FROM Venues AS a
LEFT JOIN Locations AS c
ON a.City = c.city
AND a.StateAbbr = c.state
AND c.SOYID = '" . mysql_real_escape_string($SOYID) . "'
WHERE a.SOYID = '" . mysql_real_escape_string($SOYID) . "'
OR c.SOYID IS NOT NULL /* LEFT JOIN found a matching row */
ORDER BY a.Name ASC"
EDIT: Based on comments, this version should allow you do remove the DISTINCT requirement:
"SELECT a.VenueID, a.Name, a.PhotoID, a.City, a.StateAbbr
FROM Venues AS a
WHERE a.SOYID = '" . mysql_real_escape_string($SOYID) . "'
OR EXISTS(SELECT NULL
FROM Locations AS c
WHERE a.City = c.city
AND a.StateAbbr = c.state
AND c.SOYID = '" . mysql_real_escape_string($SOYID) . "')
ORDER BY a.Name ASC"

Categories