I have a DB table with product information, and a DB table with tax rates.
My problem is that I am joining these two tables together, which works great.. until I disable "taxable" on a row for the product DB. Now my query is trying to join, but doesn't find a foreign key and I get no result at all.. I want to grab a result either way. I am using code igniter syntax, but it should be pretty obvious whats going on here:
$this->db->from('inventoryTaxRates a');
$this->db->join('inventory_items q', 'q.inventoryTaxRateID = a.inventoryTaxRateID');
sometimes q.inventyTaxRateID becomes 0, or disabled.. The query cannot join the two tables and gives me no result whatsoever. I want it to still give me the result from inventory_items.
I have tried left joining as well:
$this->db->join('inventory_items q', 'q.inventoryTaxRateID = a.inventoryTaxRateID', 'left');
You can specify a RIGHT join like this:
$this->db->join('inventory_items q',
'q.inventoryTaxRateID = a.inventoryTaxRateID',
'right');
You can use RIGHT JOIN, but I'd like to rewrite query like that:
SELECT .. FROM inventory_items ...
LEFT JOIN inventoryTaxRates ...
Related
my sql query
SELECT
team.id,
team.atitle,
team.etitle,
team.cup_id,
team.logo,
team.modarb,
team.count_id,
team.link AS tlink,
country.atitle AS name,
country.etitle AS ename,
cup.id AS catid,
cup.link,
cup.description,
cup.name AS cupname
FROM cup LEFT JOIN team ON (cup.id IN (". implode(', ', 'team.cup_id') ."))
LEFT JOIN country ON (country.id = team.count_id)
where cup.id='5'
row team.cup_id look like this 5, 4, 3,
need know how to use implode in mysql query
You should fix your data structure so you are not storing lists of ids as a string. Doing so is wrong, wrong, wrong:
It is wrong to store numbers as strings.
It is wrong to have ids in a table without properly declared foreign key references.
It is wrong to store lists in a single column, when SQL offers a great method for storing lists: a table.
Just in case you cannot fix the data structure, you can do:
FROM cup LEFT JOIN
team
ON find_in_set(cup.id, team.cup_id) > 0 LEFT JOIN
country
ON (country.id = team.count_id)
I only offer this as a stop-gap until you fix the data to be SQLish in form.
I am joining two tables: customers and queries.
I am getting the full_name from the customers table and the description from the queries table.
I am wondering if it is possible to have the results of an SQL join split into arrays that correspond with the table the data came from? For example:
$STH = $DBH->prepare("SELECT queries.description, customers.full_name FROM queries INNER JOIN customers ON queries.customer_id = customers.id");
$STH->execute();
$queries = $STH->fetchAll();
At the moment, I can access my data like this: $queries[0]['description'] and $queries[0]['full_name']
However, my question is whether there is an easy way to get the data like so: $job[0]['query']['description'] and $job[0]['customer']['full_name'].
Just as teresko mentioned, I can't understand why you'd need that.
I can only imagine you want to see on the PHP code what are the table that contained the information.
Maybe you could do something like SELECT queries.description as queries_description, then your php code would look like $queries['queries_description']. Would it be enough?
You can loop through the results in PHP and convert it to the data structure you want, but you cannot (as far as I know) automatically group the data into arrays based on the source table. A (somewhat messy) alternative, using SQL is to use a multi-query and create a temp table from your original results, then select the results on a per-table basis, like so:
CREATE TEMPORARY TABLE q AS
(SELECT queries.description, customers.full_name FROM queries
INNER JOIN customers
ON queries.customer_id = customers.id
);
SELECT q.description FROM q;
SELECT q.full_name FROM q;
So, in those SELECT statements, you'll have to list all the columns that you want for each result. Then in PHP, you'll have to iterate over each resultset and put the data into arrays (or objects/whatever) as needed. Errr. A fetchAll will still not get you what you want, but a fetchAll on the first non-empty resultset will get you all the rows from queries and the 2nd will get you all the rows from customers
I need to get info from 2 tables in one query, I searched for it on google and it came up with joins?
I got this code
mysql_query("SELECT code, url FROM apilinks, links WHERE apilinks.code='$code' and links.code='$code'");
but it doesn't seem to work, it outpus this mysql error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/codyl/public_html/projects/tests/tirl/code.php on line 18
I need to have it find url and code, in both the links table and the apilinks table, because the url has a get variable, and 'apilinks' is setup differently than 'links'
so it will have the same looking url but it has to check both tables
I don't think your error is from your SQL query, but anyway:
Your SQL query generates a cartesian product. Every row in apilinks is joined with every row in links. This is rarely what you want. You need to add a JOIN condition to the WHERE clause.
From your query, I guess code is the column you want to join on.
SELECT links.code, url
FROM apilinks, links
WHERE apilinks.code=links.code
AND links.code='$code'
Or with using an explicit join, which may be more obvious to you:
SELECT links.code, url
FROM apilinks INNER JOIN links ON apilinks.code=links.code
WHERE links.code='$code'
I think you need to specify which code column you want:
SELECT links.code, links.url
FROM apilinks, links
WHERE apilinks.code='$code'
AND links.code='$code'
Another syntax:
mysql_query("SELECT a.code, l.url FROM apilinks a, links l WHERE a.code='$code' and l.code='$code'");
Note that I am assuming the url field is in the links table.
SELECT a.code, a.url
FROM apilinks a, links l
ON ( a.code = l.code )
WHERE a.code = "$code";
I am about to begin my "Data Auditing" section of my site. What does that mean?
Well basically users can view and update records they have submitted to the database.
This is my first time doing this and I am guessing I will need to use Joins, Selects? etc.
So I was wondering if you could help me with the logic on how I will work through this query. I don't want code as I want to do it myself but the logic is what is bothering me.
Here is an image of my Database schema showing relationships etc:
http://i.imgur.com/Dju0G.png
This is what I have thought how to start (pseudocode):
-Get procedure row where procedure_id = posted value from previous
page.
-Retrieve values from that row in procedure table.
-Get patient row that matches the foreign key value in procedure table.
-Get department row that matches the foreign key value in procedure table.
-Get procedure_name row that matches the foreign key value in procedure table.
-Get dosage row that matches the foreign key value in procedure table.
However after this point I am a bit stuck on how I deal with the Many-to-Many (n--> n) relationship tables , is this where a join comes in?
What about my linking tables how do I tackle them?
If there is an easier way of doing this than what I have thought could you please tell me :)
Links to resources or examples would be much appreciated. Thanks in advance
Thankyou
Take Care!
This is the query for your pseudocode:
SELECT
procedure.*,
patient.*,
department.*,
procedure_name.*,
dosage.*
FROM
procedure
INNER JOIN
patient
ON
patient.patient_id = procedure.patient_id
INNER JOIN
department
ON
department.department_id = procedure.department_id
INNER JOIN
procedure_name
ON
procedure_name.procedure_name_id = procedure.name_id
INNER JOIN
dosage
ON
dosage.dosage_id = procedure.dosage_id
WHERE
procedure.procedure_id = ?
You can join any of your other tables exactly the same way. If you want rows back even if the table you're joining has no matching rows to the rows from the rest of the query, use a LEFT OUTER JOIN instead of an INNER JOIN. Where there are multiple rows matching the current rows (1:N/N:M) you will get as many rows as there are pairs in your result set.
If you need additional guidance/examples you'll have to be more clear about what you want to retrieve and what you don't understand.
I have this elementary query:
SELECT d.description, o.code FROM order_positions AS o
LEFT JOIN article_descriptions AS d ON (o.article_id = d.article_id)
WHERE o.order_id = 1
and I'm using MDB2 from PEAR to execute it and read the return values.
But somehow the result array always contains fields from the order_positions table only!, i.e. the result array looks like this
row[code] = 'abc123'
while I want it to look like this
row[description] = 'my description'
row[code] = 'abc123'
I already tried the following:
Vary the order of the fields, i.e. code first, then description.
Vary the order of the joined tables.
Used full table names instead of aliases.
Used the "MySQL join" instead (SELECT FROM table1, table2 WHERE table1.id = table2.id)
Used aliases with and without AS.
Some other facts:
Executing this query in MySQL Query Browser works fine, all fields are returned.
The order_positions table seems to be preferred, no matter what. When joining with additional tables I still only get fields from this table.
OK, I found the cause:
Fields with NULL values are not added to the array. In my test scenario description was in fact null and hence was not available in the array.
I'll still keep this (embarrassing) question, just in case someone else has this problem in the future.
Facepalm http://www.scienceblogs.de/frischer-wind/picard-facepalm-thumb-512x409.jpg
This should work:
SELECT d.description, o.code
FROM order_positions o, article_descriptions d
WHERE o.order_id = 1 AND d.article_id = o.article_id
Are you sure you're not using fetchOne() by mistake instead of fetchRow()?
Could you post your PHP code?
Another possibility is that in your code you missed the comma:
SELECT a b
is the same as
SELECT a AS b