Search Form with SQL from different table - php

I'm trying to create a search form for a CD collection.. We have 3 tables and all are link via ID number.. but for a start i want to get two tables working first
For the Search.php, I'm struggling with get Desc defined. I have already defined ID from both table but how do I get the ID to link to the Desc ?

Since you didnt select the catDesc in your select query which result in Undefined index error. Rewrite your code as follows
$sqlCD = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_cd.catID, nmc_category.catDesc
FROM nmc_cd
JOIN nmc_category on (nmc_cd.catID = nmc_category.catID)
WHERE nmc_cd.CDTitle = '$pCDTitle' OR nmc_cd.CDYear = '$pCDYear' OR
nmc_category.catDesc = '$pCDCategory'";
will show the result as desired. If you get any further error let me know.

I think you are trying to join 3 tables together, You almost have it.
You need to add the 2nd join to the description table like you have for the category. Then in your select statement add column from the joined table. Example below, I made assumptions about your database and column names you will need to correct these.
SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_cd.catID, nmc_description.description
FROM nmc_cd
JOIN nmc_category on (nmc_cd.catID = nmc_category.catID)
JOIN nmc_description on (nmc_cd.catID = nmc_description.catID)
WHERE nmc_cd.CDTitle = '$pCDTitle' OR nmc_cd.CDYear = '$pCDYear' OR
nmc_category.catDesc = '$pCDCategory'

Related

How to properly write query SELECT'ing multiple tables

please advise on query below. I want to select multiple tables. entryId is from table stockTracking. I bet i should use JOIN or smt.. Cheers!
$updateEntryId = $_GET["entryId"];
$query = "SELECT *
FROM stockTracking, loggers, boxes
WHERE entryId ='$updateEntryId'";
$result = mysqli_query($connection, $query);
table
boxes|boxId boxQuantity boxName
2 10 CL64
loggers | loggerId loggerQuantity loggerName
2 10 34242342
stockTracking| entryId time destination reference
2 timestamp Paris 1312
I have updated query to following, however what wrong with my WHERE statement?
As i add WHERE entryId='$updateEntryId' if fails to display any results
$updateEntryId = $_GET["entryId"];
$query = "SELECT *
FROM stockTracking
JOIN loggers
ON entryId=loggerId
JOIN boxes
ON boxId=entryId
WHERE entryId='$updateEntryId'";
Yes, you are right. You should perform a JOIN operation among-st the tables like below. replace some_column in below sample sql code with your actual column name on which you have relationship between the tables
SELECT s.*
FROM stockTracking s
JOIN loggers l ON s.some_column = l.some_column
JOIN boxes b ON b.some_column = s.some_column
WHERE s.entryId ='$updateEntryId'
Try this out:
$updateEntryId = $_GET["entryId"];
$query = "SELECT *
FROM stockTracking s
JOIN loggers l
ON s.entryId=l.loggerId
JOIN boxes b
ON b.boxId=s.entryId
WHERE s.entryId='".$updateEntryId."'";
Join the tables as stated by #rahul but should be done in proper manner .. cheers!
SOLVED.
Problem was in $updateEntryId=$_GET["entryId"];
It had no results, since as search bar was showing http://stock/edit.php?id=2, it was looking for $_GET["id"] and not $_GET["entryId"].

Distinct Values from MySQLi Query

I am trying to only show unique userIds (userIds are (0,1,2,3,4,5,6,7,8,9 etc...) for the query I am running. I tried using DISTINCT in my query, but it only shows me unique values of the rows that have 2 or more of the same userId.
Is there a way I can use php to only show the unique values. My weak points are arrays and it makes it more complicated because its using data from a MySQLi query.
Example right now I have with the query now (lets say its GROUP BY rentPaid DESC and the rent total is 800.00 for all users):
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
3--------400.00------April
1--------200.00------April
1--------100.00------April
Example desired output:
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
Can I do this with MYSQL because I tried DISTINCT and it wouldn't work, how about PHP?
Query:
SELECT
properties.*,
leases.*,
users.userId, users.primaryPhone,
CONCAT(users.userFirstName,' ',users.userLastName) AS user,
admins.adminName, payments.*
FROM
properties
LEFT JOIN leases ON properties.propertyId = leases.propertyId
LEFT JOIN assigned ON properties.propertyId = assigned.propertyId
LEFT JOIN admins ON assigned.adminId = admins.adminId
LEFT JOIN users ON properties.propertyId = users.propertyId
LEFT JOIN payments ON properties.propertyId = payments.propertyId
WHERE
payments.rentMonth = '$currentMonth' AND
payments.rentYear = '$currentYear'
Edit: Please excuse my formatting, this is my first post.
Edit: Added query....its long, but works lol. I only want unique userIds (no double or triple userIds etc...)
I suspect this is what you want:
SELECT userID, MAX(rentPaid) AS maxRentPaid, rentMonth
FROM yourTable
WHERE rentMonth = "April"
GROUP BY userID
ORDER BY maxRentPaid

MySQL very simple join example requested with subtable

I keep falling back into questions with MySQL joining.
And I would like to request a very simple example I could use to continue my journey of understanding learning the MySQL syntax.
Let's say I got the following table's
test_testtable
testtable_id
testtable_name
testtable_user
testtable_option
testtable_textfield
test_testlink
testlink_id
testlink_link
testlink_address
test_address
address_id
address_name
address_phone
address_email
address_street
address_city
address_zip
I would like to make a selection like :
SELECT * (lets say I would define the fields) FROM `test_testable`
JOIN `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
AND
JOIN `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5
Hence the linking structure is like:
test_testtable.testtable_id = leading
table test_testlink is a table to link the table test_testtable and test_address
And linking table test_testlink uses the field testlink_link to link to the table test_testtable, and uses the field testlink_address to link to the table test_address
This does not work. FOR ME.. Since I continuously seem to fail of catching the correct syntax logic.
So I hope that someone could give me a small example of how to correctly implement such a simple yet critical query!
TIAD!!
A general approach :
SELECT table1.* FROM table1
JOIN table2 ON table2.id_table1 = table1.id
JOIN table3 ON table3.id_table2 = table2.id
WHERE table1.id = 10
For your purpose :
SELECT * (lets say I would define the fields) FROM `test_testable`
JOIN `test_testlink` ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
JOIN `test_address` ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5
Please read the reference
You are using wrong syntax. You should mention which tables to join first then based on which fields.
SELECT * (lets say I would define the fields) FROM `test_testable`
INNER JOIN test_testlink
ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
INNER JOIN `test_address`
ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
AND `test_testtable`.`user_id` = 5
select * from testlink JOIN testtable ON testlink.tableid = testtable.ID
JOIN testaddress ON testlink.addressid = testaddress.ID
WHERE testtable.ID = 5

Query with join query, need assistance

I have three tables.
I combine the company and component tables with this code
$questions_query = "SELECT company_mast.id, component_mast.component_id
FROM company_mast
LEFT JOIN component_mast
ON company_mast.id = component_mast.company_id
WHERE component_mast.component_name = '".$component_name."'
AND company_mast.company_name = '".$company_name."'";
The result is as desired, If I put company_name as Bells and component_name as Assets I get and id of 3 for Bells and an id of 9 for Assets. Now if you look at the customfields table I need to pull all the questions with the a specific company_id and component_id.
Example: If the user enters Bells and Assets they need to receive all questions with the company_id of 3 and the component_id of 9.
So this is my query
SELECT *
FROM customfield_mast
LEFT JOIN ( SELECT company_mast.id, component_mast.component_id
FROM company_mast
LEFT JOIN component_mast
ON company_mast.id = component_mast.company_id
WHERE component_mast.component_name = 'Assets'
AND company_mast.company_name = 'Bells')
att
ON customfield_mast.company_id = customfield_mast.component_id
This however returns all questions in my db, which is not what I want. I'm positive my last "ON" statement is the problem, however I don't know what the correct statement would be. I have not started with SQL injection protection, this is grass roots to get my queries to work. Thanks for the help
What's wrong with another join?
SELECT company_mast.id, component_mast.component_id, CFM.DisplayName
FROM company_mast
LEFT JOIN component_mast
ON company_mast.id = component_mast.company_id
LEFT JOIN CustomField_mast CFM
ON CFM.Company_ID = Component_mast.Company_ID
and CFM.Component_ID = component_Mast.Component_ID
WHERE component_mast.component_name = '".$component_name."'
AND company_mast.company_name = '".$company_name."'";
SELECT * FROM `customfield_mast`
WHERE `company_id` =
(SELECT `id` FROM `company_mast` WHERE `company_name` = '$company_name')
AND `component_id` IN
(SELECT GROUP_CONCAT(`component_id`) FROM `component_mast`
WHERE `component_name` = '$component_name')

Mysql selection from more than one table

SELECT * FROM dog WHERE (SELECT calluser FROM jos_users WHERE `user_id`='".$cid."')=Subcode
$cid is the identifier in jos_users, which tells us which users we're fetching data about
The data I want to fetch is within "dog", and calluser is the identifier between the two (which tells us who's dogs are who's)
I need to be able to call only the dogs relevant to the user in question, but it also has to be performed in one query. Can anyone help? Much appreciated.
You need to use joins.
Read this tutorial: http://www.tizag.com/mysqlTutorial/
Your query should look something like this:
$cid = mysql_real_escape_string($_GET['cid']);
$query = "SELECT d.* FROM dog d
INNER JOIN jos_users ju ON (d.user_id = ju.id)
WHERE ju.id = '$cid' ";
If I got you right (and the id column in the dog-table links to the calluser column in the jos_user-table) the query should be
SELECT d.* FROM dog AS d JOIN jos_user AS u ON d.id = u.calluser WHERE u.user_id = '$cid'
If not please explain your data structure in more detail (maybee small ER diagram).

Categories