I am trying to join 3 tables and store the result.
The problem is I simply cannot seem to deal with it. Hours of Googling and searching led to nothing.
Here is the query:
$db = JFactory::getDBO();
$item_kit = array();
$q = "SELECT p.virtuemart_product_id,
pr.product_price,
pr.virtuemard_product_id,
pb.group_id
FROM #_virtuemart_products p
INNER JOIN #_virtuemart_product_prices pr ON p.virtuemart_product_id = pr.virtuemart_product_id,
INNER JOIN #_pb_group_vm_prod_xref pb ON pb.vm_product_id = pr.virtuemart_product_id";
$db->setQuery($q);
$item_kit = $db->loadObjectList();
return $item_kit;
Please notice that I am using the ID as the common element in all tables.
After all this nonsense occurs it should so something like:
if(!empty($item_kit['group_id'])){
echo 'It works';
}
Debugging resulted in discovering an SQL error near 'INNER JOIN #_virtuemart_product_prices pr ON p.virtuemart_product_id = pr.virtu...'
Can you please help me? Or, at least, point out where am I doing stupidly wrong...
I know it's something small and easy, but I can't see it...
Found the problem!
pr.virtuemard_product_id to pr.virtuemart_product_id
and I had to manually type in the database table prefix.
What silly of me. Thank you all for your time reading this question.
Regards,
Vlad
Remove the comma at the end of the first inner join line
INNER JOIN #_virtuemart_product_prices ... pr.virtuemart_product_id,
it should become
INNER JOIN #_virtuemart_product_prices ... pr.virtuemart_product_id
Related
I have a query...
select distinct(sod.sod_no), so.`so-no`, p.product_name, pod.prodstatus, po.`po-no`
from so_details sod
left join `sales-order` so on sod.so_number = so.`so-number`
left join products p on sod.product_id = p.product_id
left join po_details pod on sod.so_number = pod.so_number
left join `purchase-order` po on pod.po_number = po.`po-number`
where so.status = 'In Progress'
But it errors:
#1146 - Table 'po.po-no' doesn't exist
The weird thing is that it can read the sales-order table without any problems. What might be the problem?
What is this line?
left join `sales-order` so on sod.so_number = so.`so-number`, po.`po-no`
I think you can just remove the , po.po-no part. It looks like a copy-paste error.
Problem is with this line:
left join `sales-order` so on sod.so_number = so.`so-number`, po.`po-no`
I don't know why it has po.po-no. Also at this point your query still does not know about alias po.
I suggest using unified style for naming tables and columns. In SQL it's usually snake_case, but it seems like you are using _ and - in same database.
I was just wondering if I could get some pointers as to where I may be going wrong.
I have been using mysql statements and I am in the process of switching to PDO statements to use with MySQL.
I have been able to get my head around SELECT statements, but I am having a bit of trouble trying to get the insert statement to work.
I have been Googling and tried a couple of different ways to get this to work but to no avail.
This is what I have so far:
$sqlu = $conn->prepare("UPDATE ".PERSON." p
JOIN contact c ON c.personID = p.adbkid
JOIN address a ON a.personID = p.adbkid
JOIN misc m ON m.personID = p.adbkid
JOIN variables v ON v.personID = p.adbkid
SET lastname = :ln
WHERE p.pid = :id");
$sqlu->bindParam(':ln', $ln, PDO::PARAM_STR);
$sqlu->bindParam(':id', $id, PDO::PARAM_STR);
$sqlu->execute();
I have also tried it without using bindParam and using as follows:
$sqlu->execute(array('ln' => $ln, 'id' => $id));
I have also used a '?' instead of ':' and then bound the parameter or used it in the array.
When I hit the update button, I have echoed the query so I can see what is being passed through and this is what I get:
PDOStatement Object ( [queryString] => UPDATE person p JOIN contact c ON c.personID = p.adbkid JOIN address a ON a.personID = p.adbkid JOIN misc m ON m.personID = p.adbkid JOIN variables v ON v.personID = p.adbkid SET lastname = :ln WHERE p.pid = :id ) 1
I just can't see where I am going wrong. Like I say, I have Googled this and come across some answers on here too and I seem to be stuck as to where to go next.
This is a personal project I am working on and I am not looking for someone to figure this out for me, I am just looking for some pointers so I can try to fix and learn myself.
Thanks in advance.
One possibility is that the records do not have matching records in all the tables. You could try using left join. But why are you doing joins at all? Does this work?
UPDATE ".PERSON." p
SET lastname = :ln
WHERE p.pid = :id;
This assumes that lastname is in the Person table, but that seems like a reasonable assumption.
I have gone through everything and it would appear that the echoing of the query is showing the query with the placeholders and it is actually updating the database.
I'm messing around with a mysql query over 3 tables and and I just can't get it work.
The situation: I have 3 tables.
Now I try to make a mysql query based on an inputfield, where I put in a "oxid" from the table "oxarticles" and the result should be, that I get all the articles from the category where the article/oxid is which I put in the inputfield.
Example: I put "oxid" 2 in a inputfield and press submit and the result should looks like this:
Lenkrad
Reifen
Sitz
I tried a lot but never come close to. I made a day before another query that show me all the categories based on a article but I cant modified that and used it for my actual problem.
I hope you can help me :)
Thats what I get so far, but I think this is not even close cause I get a white page.
$result = mysql_query("SELECT DISTINCT oxtitle FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatnid IN (SELECT oxcatnid FROM oxobject2category WHERE oxobjectid = 2)")
or die(mysql_error()); ;
Now i got it:
The follow querys work:
SELECT DISTINCT oxtitle FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatind IN (SELECT oxcatind FROM oxobject2category WHERE oxobjectid = 2
and this one also:
select distinct
a.oxtitle
from
oxarticles a,
oxobject2category oc
where
a.oxid = oc.oxobjectid and
oxcatind in (select oxcatind from oxobject2category where oxobjectid=2
Thank you for the help :)
I am learning MySQL and messing about with joins. I am wondering if I can cut these queries down and combine them. This is what I would NORMALLY do:
$stmt = $db_pdo->prepare('SELECT date, table2_id, comments
FROM table1
WHERE user = "victor"');
$stmt->execute();
foreach ($stmt as $row) {
$table2_id = $row['table2_id'];
}
$stmt = $db_pdo->prepare('SELECT table3_id
FROM table2
WHERE table2_id = '.$table2_id.'');
$stmt->execute();
foreach ($stmt as $row) {
$table3_id = $row['table3_id'];
}
$stmt = $db_pdo->prepare('SELECT previous_comments_count
FROM table3
WHERE table3_id = '.$table3_id.'');
$stmt->execute();
foreach ($stmt as $row) {
$table3_id = $row['table3_id'];
}
(I changed the variables, so it looks stupid on purpose--I did not design the table for this company)
Basically, I need to get most of my information from the first table, but there is one "id" that needs to cross two tables that I would like to put into the first query.
So what I tried was:
$stmt = $db_pdo->prepare('SELECT date, table2_id, comments
FROM table1
LEFT JOIN table2
ON table1.table2_id = table2.table2_id
LEFT JOIN table3
ON table2.table3_id = table3.table3_id
WHERE user = "victor"');
And obviously my first (and many tweaked versions of this line) have failed, leading to a blank fetch.
I can do the first left join, but even that doesn't seem to show the information from the second table. The second and third tables do not have the same columns as the first table. (I don't think this is a problem but I could be wrong.)
I've done some reading on various websites, and though I love figuring out my problems on my own, I think it's best to ask if what I'm trying to accomplish is even possible and to try and get it done ASAP. I've read some other questions like this, but I am just simply befuddled looking at the complexity of this let alone those ones. (It's one of those days...)
It could be that I'm also using pdo and prepared statements for the first time as well, I do NOT think that's the problem, but I apologize if it is. I'm not a MySQL genius and have not spent much time with databases.
Also (side question, if permitted): Do I have to use foreach? Or can I just trust that calling $stmt after the execute is good enough? (AKA passes true/false on the ONE result that should show up)
Try the joins like this:
table3
LEFT JOIN table2
ON table3.table3_id = table2.table3_id
LEFT JOIN table1
ON table2.table2_id = table1.table2_id
basically i need to write a query for mysql, but i have no experience in this and i cant find good tutorials on the old tinternet.
i have a table called rels
with columns "hosd_id" "linkedhost_id" "text link"
and a table called hostlist
with columns "id" "hostname"
all i am trying to achieve is a query which outputs the "hostname" and "linked_id" when "host_id" is equal to "id"
any help or pointers on syntax or code would be helpfull, or even a good mysql query
guide
I always thought w3schools and Tizag tutorials were pretty good for beginners...
http://www.w3schools.com/sql/default.asp
http://www.tizag.com/mysqlTutorial/
Try:
SELECT hostname, linkedhost_id
FROM rels, hostlist
WHERE host_id = id;
This should do the trick;
SELECT hostname, linked_id FROM hostlist, rels WHERE rels.host_id = hostlist.id
Try:
SELECT h.hostname, r.linkedhost_id
FROM rels r
INNER JOIN hostlist h ON h.id = r.hosd_id
The MySQL documentation has a section on SQL Syntax that is a good start for learning how to write SQL queries.
Try this:
select h.hostname, r.linkedhost_id from rels r inner join hostlist h on
r.hosd_id = h.id where r.host_id = hostlist.id
Finally, have a look at basics of mysql.
Everyone has answered this question correctly, but i also want to post an answer to this.
Here's mine:
SELECT hostlist.hostname, rels.linkedhost_id
FROM rels
INNER JOIN hostlist ON (hostlist.id = rels.host_id)
WHERE rels.host_id = hostlist.id;