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).
Related
I am trying to make a "recipe" system inside a game. The player can own a company and craft items in there.
I currently fetch the recipes per company type but I don't know how to write the query in a way that I can also fetch the item names and images if the item_id is not empty.
This is working:
SELECT a.recipe_id,
a.item1_id,
a.item1_uses,
a.item2_id,
a.item2_uses,
a.item3_id,
a.item3_uses,
a.item4_id,
a.item4_uses,
a.item5_id,
a.item5_uses,
a.newitem_id,
a.newitem_uses,
a.craft_description,
a.craft_button
FROM
company_recipes AS a,
company_types AS b
WHERE
a.type_id = b.type_id
AND
b.type_id = '".$type."';
"
A recipe can contain for example two items needed to craft something new, but it could also be 5. So if it's only 2, I only want to fetch the img, name of these 2 and the rest can be skipped.
I have a different table store_items that contains the img and name of the item. I was thinking something along the lines of an IF ELSE or CASE WHEN inside the query, but I'm not sure how I'd do that.
Something like: SELECT c.img, c.name FROM store_items AS c IF a.item1_id is not NULL.
I feel like I'm close to the solution, but missing the last step.
Thanks for the tips #jarlh, I've changed the code and came to this result. If you have any more tips to do it better I'm happy to listen. (I'm still a junior and thought myself by trial and error, so I might not have the best solutions at times... Which is why tips are highly appreciated).
SELECT cr.recipe_id,
cr.item1_id,
cr.item1_uses,
si1.name,
si1.img,
cr.item2_id,
cr.item2_uses,
si2.name,
si2.img,
cr.item3_id,
cr.item3_uses,
si3.name,
si3.img,
cr.item4_id,
cr.item4_uses,
si4.name,
si4.img,
cr.item5_id,
cr.item5_uses,
si5.name,
si5.img,
cr.newitem_id,
cr.newitem_uses,
si_new.name,
si_new.img,
cr.craft_description,
cr.craft_button
FROM
company_recipes AS cr
INNER JOIN company_types AS ct ON cr.type_id = ct.type_id
LEFT JOIN store_items AS si1 ON cr.item1_id = si1.item_id
LEFT JOIN store_items AS si2 ON cr.item2_id = si2.item_id
LEFT JOIN store_items AS si3 ON cr.item3_id = si3.item_id
LEFT JOIN store_items AS si4 ON cr.item4_id = si4.item_id
LEFT JOIN store_items AS si5 ON cr.item5_id = si5.item_id
LEFT JOIN store_items AS si_new ON cr.newitem_id = si_new.item_id
WHERE
ct.type_id = '".$type."';
I'm basically fetching everything now and handle the NULLs in the php code now.
Without seeing more info its had to see what you are trying achieve but you could start by using the the users inpute of the game to determine what data is first required before futher filtering. Try this:
Declare #Value int
set #Value = #User_input --- uses what ever the game user will
SELECT
a.recipe_id,
a.item1_id,
a.item1_uses,
a.item2_id,
a.item2_uses,
a.item3_id,
a.item3_uses,
a.item4_id,
a.item4_uses,
a.item5_id,
a.item5_uses,
a.newitem_id,
a.newitem_uses,
a.craft_description,
a.craft_button
--- you can insert more columns but i stopped here as i dont know what data you have in the other tables.
FROM
company_recipes a
INNER JOIN company_types b ON a.type_id = b.type_id
INNER JOIN store_items c ON c.type_id = b.type_id
WHERE
b.type_id = #Value; --- '".$type."';
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"].
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
I've been Googling for most of my day now but I can't seem to find the right answer. Maybe because I don't really know in which direction to look (join? exist?). What I have is 3 tables:
There is a table named 'groups' and 'languages' containing the actual group- and language data but that's not important right now.
The user should be able to generate a list with all members, depending on the selected groups and/or languages. The groups/languages that the user selected are saved in two separate array's containing the IDs ($aGroups and $aLangs).
What I want/need is to SELECT * FROM members WHERE ...
And that's where I got stuck. I've tried joins, I've tried IN(), I've tried EXIST but nothing seems to work right.
Any help is greatly appreciated!
If you want to select members by languages and groups, you can do something like this :
$query = "select * from members as m
left join group_members as gm on gm.member_id = m.id
left join language_members as lm on lm.member_id = m.id";
if(isset($aGroups)) {
$query .= " where group_id in (".implode(",", $aGroups).")";
if(isset($aLangs)) {
$query .= " and language_id in (".implode(",", $aLangs).")";
}
}
elseif(isset($aLangs)) {
$query .= " where language_id in (".implode(",", $aLangs).")";
}
I think you just need some brackets:
//assuming you have ids stored in $lang and $group
SELECT * FROM members WHERE (SELECT group_id FROM group_members WHERE member_id=members.id)='$group'
AND (SELECT lang_id FROM language_members WHERE member_id=members.id)='$lang'
The trick is "members.id", DB will call subquery for every member to find out if the condition for group and lang is met.
Select m.* FROM members m, group_members gm, language_members lm
WHERE
lm.member_id=m.id AND
m.id=gm.member_id AND
<<<< START YOUR OWN WHERE HERE.
I have two tables, Table A and Table B. For each record in table A there are many records in Table B; thus, a one to many relationship exists between tables A and B. I want to perform a query so that for each row returned from table A, all of the corresponding rows will be returned from table B. From what I understand I'll need to use a INNER Join - however, how would I go about accessing all of the returned rows through say, PHP?
$sql = "Select A.ID, B.Name * From A INNER JOIN B ON A.ID = B.ID";
A.ID | B.fName | B.lName
1 nameone lnameone
1 nametwo lnametwo
2 namethree lnamethree
4 namefour lnamefour
Now that I have the above results, I want to use PHP to loop through all of the values of B.Name only for a single A.ID at a time. So, the results I want would look like:
1.
nameOne lNameOne
nameTwo lnametwo
2. namethree lNamethree
4. nameFour lNameFour
Basically, I'm trying to group the query results by the ID in table A.
I appreciate the help very much!
Thank you,
Evan
You could just Google "php get from database," and use some normal array pre-processing, but I worry the advice you find may not be ideal. Here's what I'd do:
$pdo = new PDO('mysql:host=host;dbname=dbname', 'user', 'pass');
$result = $pdo->query(<<<SQL
SELECT
ID,
Name
FROM
A
-- Alternative to `JOIN B USING(ID)` or `JOIN B ON (A.ID = B.ID)
NATURAL JOIN B
SQL
);
$a = array();
while ($row = $result->fetch()) {
if (!isset($a[$row['ID']]) {
$a[$row['ID']] = array();
}
$a[$row['ID']][] = $row['Name'];
}
You could also GROUP BY the ID and GROUP_CONCAT the names to be exploded in PHP later to skip the manual array creation and reduce some iteration (although SQL will do more in that case).
Adding a simple ORDER BY A.ID to the SQL query would probably get you quite far in "grouping" the items together. It's difficult to give a more detailed answer without knowing exactly what you want to do with the "groups".