This is was am trying to do table A has all profession categories & table B has users details.i want select from the 2 tables and match up categories selected,if user select 'web developer' from a dropdown of categories.i want to display list of users under web developer.
<?php
$q = intval($_GET['q']);
//select state of the two tables here
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
?>
///
<?php }?>
it's unclear from the question and lack of database details what and how to do the sql query ( which I think is what you are actually asking for ) but something along these lines perhaps.
select * from `tableB` b
left outer join `tableA` a on a.`professionid`=b.`professionid`
where a.`profession`='Web Developer'
Related
I have a table named purchase_details_master and another is sales_detais_master Where all the purchase and sales details are available.
Structure of Purchase Table: These are the data which I have purchased
Structure of Sales Table: These are the data which I have sell
Now I need to show the data like this, This image is showing the iPhone 12 data. In the Inward field showing tha pieces of purchased item, In the Outward field showing the data of sells pieces, and the closing data showing like Opening + Inward or Opening - Outward = Closing
I am using Core Php with Sqlite database
$item = $_POST['item'];
$sql = "SELECT * FROM purchase_details_master WHERE item_id = '$item'";
$sql_run = $conn->query($sql);
I have used while loop it's not solving the problem
Kindly solve this out for me! Thank you in Advance
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
like that.
SELECT * FROM purchase_details_master AS pdm INNER JOIN sales_detais_master AS sdm ON sdm.item_id = pdm.item_id WHERE sdm.item_id = ?
this will select all fields from the two tables
$sql = "SELECT * FROM purchase_details_master, sales_detais_master WHERE purchase_details_master.item_id = '$item' AND purchase_details_master.item_id = sales_details_master.item_id";
or select exact fields from the two tables
SELECT table1.field1, table1.field2, table2.field1, table2.field3
FROM table1, table2,
WHERE table1.field1 = something, or table1.field1 = table2.field2
I have 2 tables, one is called companies (with columns: title, finance, address, phone....) and the other is called investments (with columns: title, budget....).
The title value is same in both tables example: google inc as title is stored in both tables.
All I want to do is display data from table "investments" to a page called companies-profile using title as key.
companies-profile page shows data based on id that I get from another page (where all companies are displayed).
I use this code:
<?php
$var1 = "SELECT title
FROM companies
WHERE idcompanies='$ID'";
$result2 = mysqli_query($conn, "SELECT budget
FROM n4399
WHERE title='{$var1}'")
or die(mysqli_error($conn));
$row2 = mysqli_fetch_array($result2);
echo $row2['budget'];
?>
$conn is declared and database conection is ok
i m getting:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '2'" at line 1
2 is the id that is selected by the user
Using xamp
You could use a single query
SELECT budget
FROM companies
INNER JOIN n4399 ON companies.Title=n4399.title
WHERE companies.id = ?
and using a proper prepared statement and binding
$stmt = $conn->prepare("SELECT budget
FROM companies
INNER JOIN n4399 ON companies.Title=n4399.title
WHERE companies.id = ?");
$stmt->bind_param("i", $ID);
$stmt->execute();
//
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
echo $row['budget'];
}
You are trying to execute the following sql sentence:
SELECT budget
FROM n4399
WHERE title='SELECT title
FROM companies
WHERE idcompanies='$ID''
Either of two: or you first execute the firs sentence (select title from companies...) getting the effective title of the company or you can change your select into this:
SELECT budget
FROM n4399
WHERE title in (SELECT title
FROM companies
WHERE idcompanies='$ID')
So that this select can return all the values that have matching titles in the second select.
I have to select data from multiple tables based on single key value. I have one table called maintable where I will get all the ids from, and i have another 10 tables in the same database which have maintable.id as a foreign key. Now I have to retrieve data from the 10 tables where maintable.id matches in one single table.
The code I have tried is:
$sql = select id from maintable;
$runsql = mysql_query($sql);
while($sqlRow = mysql_fetch_array($runsql ,MYSQL_ASSOC)) {
for($i=1;$i<=10(This is another table count);$i++) {
$servSql = "select * from table.$i where ref_id = ".$sqlRow['id'];
$runServerSql = mysql_query($servSql);
while($serverRow = mysql_fetch_array($runServSql,MYSQL_ASSOC)) {
}
}
}
Try something like this in a join:
SELECT * FROM maintable m
INNER JOIN othertable o
ON m.id = o.id
That will select from both tables using an inner join ON the id column. You may want to look up a basic SQL tutorial to learn the basic types of joins you can use. Good luck!
I have two tables
1.owners
id
firstname
lastname
2.product
id
id2
id3
item
SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='Jezebel';
Works fine from the command line returning all relevant items from owners and product but using the following PHP
$result = mysql_query("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='".$_POST['fname']."'")
only returns results from the table owners.
I have googled extensively and don't see anyone else with this problem.
$fname= $_POST['fname'];
$result = mysql_query("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='$fname'")
First, make your query similar to this
$query = sprintf("SELECT * FROM owners LEFT JOIN product ON product.id=owners.id where firstname='%s'",mysql_real_escape_string(trim($_POST['fname'])));
$result = mysql_query($query) or die(mysql_error());
var_dump(mysql_fetch_assoc($result));
and let me know the result so I can update this answer.
First of all: You have two table which match on autoincrement column id?
However, try this?
SELECT tb1.id, tb1. firstname, tb1.lastname, tb2.id, tb2.id2, tb2.id3, tb2.item FROM owners AS tb1 LEFT JOIN product AS tb2 ON tb2.id=tb1.id where tb1.firstname=$fname
I have found the error. It was nothing to do with the select statement. I had a typo in the display code. It should have been like this
echo "</td><td>";
echo $row['id3'];
But was like this instead
echo "</td></td>";
echo $row['id3'];
Thank you all for your help.
Ok, here's a database.
http://i.stack.imgur.com/j05AB.png
Say I've inserted values into the database for each of these tables, although the IDs would be auto incrementing. There are many BVALUES from each AVALUE, thus the AB table. I have all the AVALUEs from TABLE A in a drop-down list. A user selects an AVALUE which I put into a variable using
$AVALUE = $_POST['AVALUE']
Then I do an sql statement to get the AVALUEs from TABLE A that equal $AVALUE.
$sql = "SELECT AVALUE FROM TABLEA WHERE" . $AVALUE . " = AVALUE";
How do I then get the NAMEID from TABLEA for that AVALUE, then reference to AB where TABLEANAMEID = NAMEID from TABLEA? Then I want to get all the BVALUES by getting all the TABLEBNAMEIDs that correspond to the TABLEANAMEIDs.
I then want those BVALUES in a drop-down list on a seperate HTML page. After a bit of Googling the solution I think would be to do some sort of a loop putting the BVALUES into a variable as all the NAMEIDs from TABLE B increment where the variable would be in an $BVALUE loop and the list values would show with all the BVALUES.
I hope I explained that right. I think I know what I'm trying to do but I have no idea how to actually implement it. Please help guys.
You need to join those table together. What you are describing is an m:n relation. In this case you have do use 2 joins like this:
SELECT * FROM TableA AS a WHERE a.avalue = $AVALUE
JOIN TableAB AS a2b ON a.namevalue = a2b.id_a
JOIN TableB AS b ON a2b.id_b = b.id
Maybe u means, u want to get all BVALUE which has relation with selected AVALUE in table AB
$sql = "SELECT B.NAMEID as id, BVALUE as value FROM TABLEA A
LEFT JOIN AB ON TABLEANAMEID=A.NAMEID
LEFT JOIN TABLEB B ON TABLEBNAMEID=B.NAMEID
WHERE AVALUE = $AVALUE";`
get mysql result
$result = mysql_query($sql);
iterate
echo "<select>";
foreach ($r = mysql_fetch_object($result)) {
echo '<option value="'.$r->id.'">'.$r->value.'</option>';
}
echo "</select>";