I researched this a bit already and all my attempts have come up short so far. I am trying to perform a mysql query in my php script that deals with multiple tables.
Here is what the tables look like:
TABLE 1
name
TABLE 2
Product (name)
Inventory
CatID
ProductID
TABLE 3
product_url
"name" (Table 1) must be the sane as "Product" (Table 2). Next, "Inventory" (table 2) must be = to "Y". Lastly, "CatID" must be = "2".
My attempt looked somewhat like this:
SELECT 1.name, 2.Product, 2.Inventory, 2.CatID
FROM table1 1, table2 2
WHERE 2.Inventory = 'Y'
AND 1.name = 2.Product
AND 2.CatID = '2'
From the results, I would be looking to get more information from the table such as product description, etc which would be in table1 and table2... I have never joined or queried 2 (or more) tables before. Any help would be greatly appreciated.
Try this:
SELECT table1.Name, table2.Product, tabl2.Inventory, table2.CatID
FROM table1 INNER JOIN table2
ON table1.Name = table2.Product
WHERE table2.CatID = '2'
SELECT t1.name, t2.Product, t2.Inventory, t2.CatID, t2.ProductID
FROM table1 t1
INNER JOIN table2 t2 ON t2.Product = t1.name
WHERE t2.Inventory = 'Y' AND t2.CatID = 2
I'm sorry to say that the database you have to work with was very poorly designed. If the query I gave you doesn't work, then make sure you have data in the tables that actually meets the criteria you're looking for.
Also remember that when you're accessing these fields in PHP, capitalization matters. You need to do something like this:
<?php
$q = QUERY FROM ABOVE
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)) {
$name = $row["name"];
$product = $row["Product"];
$inventory = $row["Inventory"];
$catid = $row["CatID"];
$productid = $row["ProductID"];
}
?>
Related
I had did the below code to show the given answer number from answer table and to show answer value and the right answer value from question bank table. But I need it to do the code of two distinct MySQL query in a single query some thing like this.
$result = mysql_query("SELECT t1.*, t2.(t1.clicked_answer)
FROM Eg_Net_Solution_test_answer AS t1,
Eg_Net_Solution_test_question_bank AS t2
WHERE t1.user_serial = '10' AND
t1.area='Hamdun Pur' AND
t1.question_no=t.question_no");
Is it possible to do the below code in a similar method like the upper one?
`
<?php
$given_answer_value="";
$right_answer_value="";
$question_no="";
$given_answer_no="";
$right_answer_no="";
$result=mysql_query("SELECT * from Eg_Net_Solution_test_answer where user_serial='10' AND area='Hamdun Pur'" );
while($row=mysql_fetch_array($result))
{
$question_no=$row['question_no'];//question_no is a Column of Eg_Net_Solution_test_answer table
$given_answer_no= $row['clicked_answer']; //clicked_answer is a Column of Eg_Net_Solution_test_answer table contains value a,b,c,d
$result2=mysql_query("SELECT * from $Eg_Net_Solution_test_question_bank where question_no='$question_no'" );
while($row2=mysql_fetch_array($result2))
{
$given_answer_value=$row2[$given_answer];// $given_answer is Column of Eg_Net_Solution_test_question_bank table and contains string value. Like $given_answer=a, and this colun a contains value "Prophet Muhammad RIP"
$right_answer_value=$row2[right_answer];// right_answer is Column of Eg_Net_Solution_test_question_bank table contains vale a,b,c,d
}
echo $row['question_no'];
echo "(".$row['answer']."). ".$given_answer_value;
echo "(".$row['right_answer']."). ".$right_answer_value;
}
?>
If I am not wrong, you are just meant to do a INNER JOIN between both the tables on question_no column. Something like
SELECT t1.*, t2.clicked_answer
from Eg_Net_Solution_test_question_bank t1
join Eg_Net_Solution_test_answer t2 on t1.question_no = t2.question_no
where t2.user_serial = '10'
and t2.area = 'Hamdun Pur';
i'm in the process of joining two tables together under two different conditions. For primary example, lets say I have the following nested query:
$Query = $DB->prepare("SELECT ID, Name FROM modifications
WHERE TYPE =1 & WFAbility = '0'");
$Query->execute();
$Query->bind_result($Mod_ID,$Mod_Name);
and this query:
$Query= $DB->prepare("SELECT `ModID` from `wfabilities` WHERE `WFID`=?");
$Query->bind_param();
$Query->execute();
$Query->bind_result();
while ($Query->fetch()){ }
Basically, I want to select all the elements where type is equal to one and Ability is equal to 0, this is to be selected from the modifications table.
I further need to select all the IDs from wfabilities, but transform them into the names located in modifications where WFID is equal to the results from another query.
Here is my current semi-working code.
$Get_ID = $DB->prepare("SELECT ID FROM warframes WHERE Name=?");
$Get_ID->bind_param('s',$_GET['Frame']);
$Get_ID->execute();
$Get_ID->bind_result($FrameID);
$Get_ID->fetch();
$Get_ID->close();
echo $FrameID;
$WF_Abilties = $DB->prepare("SELECT ModID FROM `wfabilities` WHERE WFID=?");
$WF_Abilties->bind_param('i',$FrameID);
$WF_Abilties->execute();
$WF_Abilties->bind_result($ModID);
$Mod_IDArr = array();
while ($WF_Abilties->fetch()){
$Mod_IDArr[] = $ModID;
}
print_r($Mod_IDArr);
$Ability_Name = array();
foreach ($Mod_IDArr AS $AbilityMods){
$WF_AbName = $DB->prepare("SELECT `Name` FROM `modifications` WHERE ID=?");
$WF_AbName->bind_param('i',$AbilityMods);
$WF_AbName->execute();
$WF_AbName->bind_result($Mod_Name);
$WF_AbName->fetch();
$Ability_Name[] = $Mod_Name;
}
print_r($Ability_Name);
See below:
SELECT ModID,
ID,
Name
FROM modifications M
LEFT JOIN wfabilities WF
ON WF.ModID = M.ID
WHERE TYPE =1 & WFAbility = '0'
To do this, you need to join your tables, I'm not quite sure what you are trying to do so you might have to give me more info, but here is my guess.
SELECT ID, Name, ModID
FROM modifications
JOIN wfabilities
ON WFID = ID
WHERE TYPE = '1'
AND WFAbility = '0'
In this version I am connecting the tables when WFID is equal if ID. You will have to tell me exactly what is supposed to be hooking to what in your requirements.
To learn more about joins and what they do, check this page out: MySQL Join
Edit:
After looking at your larger structure, I can see that you can do this:
SELECT modifications.Name FROM modifications
JOIN wfabilities on wfabilities.ModID = modifications.ID
JOIN warframes on warframes.ID = wfabilities.WFID
WHERE warframes.Name = 'the name you want'
This query will get you an array of the ability_names from the warframes name.
This is the query:
"SELECT A.ID, A.Name,B.ModID,C.Name
FROM modifications as A
LEFT JOIN wfabilities as B ON A.ID = B.WFID
LEFT JOIN warframes as C ON C.ID = B.WFID
WHERE A.TYPE =1 AND A.WFAbility = '0' AND C.Name = ?"
My code right now is this
$extract = mysqli_query($dbc, "SELECT * FROM items");
$numrows = mysqli_num_rows($extract);
while($row = mysqli_fetch_assoc($extract)) {
$id = $row['i_id'];
$iname = $row['i_name'];
echo "<tr><td><a href='capture.php?item_id=$id'>$iname</a></td><td>Incomplete</td></tr>";
}
What i want to do is run a check to see if the item has already been completed.
I have a separate table that contains the information, for instance say that Item 1 has been completed then I would like to be able to change my echo to something like:
echo "<tr><td>$iname</td><td>Complete!</td></tr>";
How would I accomplish this? Would I need to use some form of a join statement?
I want to be able to display all the items in the table but not duplicate them i initially thought that an if statement on the echo to see if item complete then do this else do that
Here are the two tables
Items item_collection
------ ---------------
i_id i_id
i_name complete
caption
image
c_id
g_id
You can use join condition like this (assuming complete is a varchar field)
SELECT a.i_id, a.i_name,
CASE WHEN i_status = '1' THEN 'Complete!' ELSE 'Incomplete' END AS complete_status
FROM items a
LEFT OUTER JOIN item_collection b ON a.i_id = b.i_id
select
case
when ic.complete = 1 then 'Complete'
else 'Incomplete'
end as item_status
from items i
left join item_collection ic on i.i_id = ic.i_id
where i.i_name = 'your_item_name'
Assuming that ic.complete = 1 when item is complete.
Something along the lines of SELECT * FROM table1 WHERE table1.id not in (SELECT Id FROM table2)
I have an array with a lot of data from a database. In the array, there are two fields called teameinsid and teamzweiid. The query is a very simple one :
$spiel = mysql_query("select teameinsid, teamzweiid from begegnung", $connection) or die("Keine passende Begegnung");
What I need is to search in the database for the names of these IDs. The names are in a different table.
What I have now is the following:
while($tmp = mysql_fetch_array($spiel)){
$teins = $tmp['teameinsid'];
$tzwei = $tmp['teamzweiid'];
}
So I know the two IDs, but I don't know where to save the names. If I try:
$name = mysql_query("select name from team where teameinsid = $teins", $con)
it gets overwritten every time. How can I manage this?
EDIT:
Database Scheme:
Table Team : id, name
Table Begegnung: id, teameinsid, teamzweiid
I tested this, and it works if your query in $speil is this:
SELECT B.*, T1.name AS teamnameeins, T2.name as teamnamezwei FROM begegnung AS B
JOIN team AS T1 ON T1.id = B.teameinsid
JOIN team AS T2 ON T2.id = B.teamzweiid
http://dev.mysql.com/doc/refman/5.0/en/join.html
You should use your code now like this:
while($tmp = mysql_fetch_array($spiel)){
$teins = $tmp['teamnameeins'];
$tzwei = $tmp['teamnamezwei'];
}
change your code like this
while($tmp = mysql_fetch_array($spiel)){
$teins[] = $tmp['teameinsid'];
$tzwei[] = $tmp['teamzweiid'];
}
now
$ids=implode ( ',' , $teins);
$name = mysql_query("select name from team where teameinsid in($ids)", $con)
I am looking for a cleaner way to do this. My code works, but I know it can be better. I have three tables: one with a list of Category Groups, One with a list of categories that are linked to category groups, and one with a list of news stories that are linked to the categories.
I need to loop through all of the names of the Category Groups, followed by the names of the categories that are in the category groups, with the number of news stories in each category listed as well.
I have three tables: CategoryGroups, Categories, News.
I have a set of queries. The first queries all the rows from the CategoryGroups table:
$result = mysql_query( '
SELECT cat_group_id, cat_group_name FROM CategoryGroups
' );
The second query is inside the looped results of the first query and finds all the categories that have a news item and are linked to a specific category group:
<?php
while( $row = mysql_fetch_assoc( $result ) ){
$id = $row['cat_group_id'];
$name = $row['cat_group_name'];
echo "<h3>$name</h3>";
$sql = mysql_query("
SELECT category_id, title FROM `Categories`
WHERE cat_group_id = $id
AND category_id IN
(SELECT news.category_id FROM news)
");
while( $row = mysql_fetch_assoc($sql) ) {
$title = $row['title'];
$catid = $row['category_id'];
$numbers = mysql_query("
SELECT * FROM news
WHERE category_id =$catid"
);
$nums = mysql_num_rows($numbers);
echo "$title ($nums)<br/>\n";
}
?>
I would like to limit this to one or two queries, with efficiency in mind. I know this can be done, however I have not been successful in my attempts.
thanks.
Why not JOIN the tables?
SELECT cat_group_name, title, count(newsid)
FROM CatagoryGroups
INNER JOIN Categories ON cat_group_id
INNER JOIN News ON category_id
GROUP BY cat_group_name, title
looks like it should be close, if table news has a newsid column (it's gotta have SOME primary key, right? well, count that;-). With the obvious indexes the JOINs should be quite fast, and your PHP code can do whatever output formatting you may need from that.
I suggest you need to get a book on SQL, such as "SQL Queries for Mere Mortals."
$sql = mysql_query("
SELECT cg.cat_group_name, c.title, COUNT(n.category_id) AS NumNews
FROM `CategoryGroups` cg
JOIN `Categories` c USING (cat_group_id)
JOIN `News` n USING (category_id)
GROUP BY cg.cat_group_name, c.title");
Then loop over the result and output a new <h3> each time the cat_group_name is different from the previous row.