The title is a bit confusing, hopefully it is clear what I am trying to do from my query. I thought I understood joins, clearly not, here is my $query:
SELECT DATE(T0.timestamp),
SUM(T0.total_responses),
SUM(T0.responses),
T0.metric_id,
T1.metric_id
FROM `personal_aggregates` AS T0
LEFT JOIN `qrs_metrics` AS T1
ON T0.metric_id = T1.qrs_metric_id
WHERE T0.user_id = 1 AND
T0.duration = '1' AND
T0.category_id IN (1,2,3,4) AND
T0.timestamp >= 'period_duration'
GROUP BY DATE(T0.timestamp)";
I am trying to join the tables on the columns metric_id and qrs_metric_id. However I get no results from the above query. When I loop through the result in PHP and check any of the variables, for example $result['T0.metric_id'], I get Undefined index: T0.metric_id
If anyone can shed any light on this I would much appreciate it.
Try this:
$query = "SELECT DATE(T0.timestamp), SUM(T0.total_responses), SUM(T0.responses),
T0.metric_id AS metric0, T1.metric_id AS metric1
FROM `personal_aggregates` AS T0
LEFT JOIN `qrs_metrics` AS T1 ON T0.metric_id = T1.qrs_metric_id
WHERE T0.user_id = 1 AND T0.duration = '1'
AND T0.category_id IN (1,2,3,4)
AND T0.timestamp >= 'period_duration'
GROUP BY DATE(T0.timestamp)";
you can now make a difference by using this:
echo $row['metric0'];
echo $row['metric1'];
Either you need to add some aliases to your selected columns
SELECT DATE(T0.timestamp) as tstamp, SUM(T0.total_responses) as responses...
and refer to the columns by alias:
$result['tstamp']
or you can refer to the columns by numeric index:
$result[0]
Your query should be
$query = "SELECT
DATE(T0.timestamp) AS T0timestamp,
SUM(T0.total_responses) AS total_responses,
SUM(T0.responses) AS responses,
T0.metric_id,
T1.metric_id AS metric_id_2
FROM `personal_aggregates` AS T0
And accessed like
$result['metric_id'];
$result['metric_id_2'];
$result['T0timestamp'];
$result['total_responses'];
Exclude the table alias from the associative array access e.g. "T0.XXX" is wrong. You can only do that in the SQL
Related
include '../incs/connect.php';
$q=mysqli_query($con,"SELECT appl_reg_details.appl_id, appl_reg_details.apl_name, appl_reg_details.apl_email, rp_test_result.appl_status, rp_intv_result.intv_obt_marks
FROM appl_reg_details
INNER JOIN rp_test_result ON appl_reg_details.appl_id = rp_test_result.appl_id
INNER JOIN rp_intv_result ON rp_test_result.appl_id = rp_intv_result.appl_id
GROUP BY appl_reg_details.appl_id
");
WHILE($row=mysqli_fetch_array($q))
{
echo $row=['appl_id']." - ";
echo $row=['apl_name']." - ";
echo $row=['intv_obt_marks']." - ";
echo $row=['appl_status']." -";
echo $row=['apl_email']."<br/>";
}
I have 3 tables and i want to get data of 3 field from table1, of 1 field from table 2 and table 3 each, i have found that inner join can do this, when i run this it says 'Notice: Array to string conversion',
help to correct this or give a new way please
The query looks fine at first glance.
The problem is with the lines below it. They include an unnecessary =:
echo $row=['apl_email']."<br/>";
These lines should be like this:
echo $row['apl_email']."<br/>";
Performance benefits aside, GROUP BY serves no purpose here other than to confuse the result set. Try something like this instead, and fix the coding errors identified by rjdown:
SELECT DISTINCT d.appl_id
, d.apl_name
, d.apl_email
, t.appl_status
, i.intv_obt_marks
FROM appl_reg_details d
JOIN rp_test_result t
ON t.appl_id = d.appl_id
JOIN rp_intv_result i
ON r.appl_id = i.appl_id;
EDIT: Strange I vardumpped every variable they are all a bool(false)
I'm trying to make this query. But when I click the search button nothing shows up.
Someone who can direct me in the right direction? Thanks in advance.
$STH = $DBH->query("SELECT * FROM artikel
LEFT JOIN barcode
ON artikel.barcode_id = barcode.barcode_id
LEFT JOIN debiteur
ON artikel.debiteur_id = debiteur.debiteur_id
WHERE debiteur.debiteur_naam LIKE '%$formulier%'
AND artikel.artikel_leverancier LIKE '%$leverancier%'
AND artikel.artikel_leverancier_code LIKE '%$artikelleverancier%'
AND artikel.artikel_code LIKE '%$artikelexact%'
AND artikel.artikel_omschrijving LIKE '%$artikelomschrijving%'
ORDER BY '$orderby' ASC");
Assuming you are interested in items which satisfy atleast one LIKE condition, you need to replace ANDs with ORs
$STH = $DBH->query("SELECT * FROM artikel
LEFT JOIN barcode
ON artikel.barcode_id = barcode.barcode_id
LEFT JOIN debiteur
ON artikel.debiteur_id = debiteur.debiteur_id
WHERE debiteur.debiteur_naam LIKE '%$formulier%'
OR artikel.artikel_leverancier LIKE '%$leverancier%'
OR artikel.artikel_leverancier_code LIKE '%$artikelleverancier%'
OR artikel.artikel_code LIKE '%$artikelexact%'
OR artikel.artikel_omschrijving LIKE '%$artikelomschrijving%'
ORDER BY '$orderby' ASC");
It sounds like you have to test for empty parameters. Here is one way in SQL:
WHERE ('$formulier' <> '' and debiteur.debiteur_naam LIKE '%$formulier%') or
('$leverancier' <> '' and artikel.artikel_leverancier LIKE '%$leverancier%') or
('$artikelleverancier' <> '' and artikel.artikel_leverancier_code LIKE '%$artikelleverancier%') or
('$artikelexact' <> '' and artikel.artikel_code LIKE '%$artikelexact%') or
('artikelomschrijving' <> '' and artikel.artikel_omschrijving LIKE '%$artikelomschrijving%')
Alternatively, you can build up the query clause by clause when the variables are not empty.
Fiddle with tables here
I'm using the following sql with the tables in the fiddle to check if a user has reached the borrowing limit. The problem here is, If an invalid item number were supplied it returns NULL, if a user has not borrowed any items, it returns NULL. This way, I cannot tell if a invalid item number were supplied or if a user actually has not borrowed any books. What would be a good way to check if a invalid item number was supplied or a member actually has not borrowed anything under that category?
set #mId = 3 //Has not borrowed anything till now.
set #id = 21; //This item does not appear in the collection_db table and is therefore invalid.
set #country = 'US';
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder and col1.country = lend.country
WHERE col1.id = #id and col1.country = #country
AND col2.category = 10
AND lend.memId = #mId and lend.country = #country
The furthest I could get with the one query is (had to take out php and "country" vars for fiddle to work):
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
,case when valid1.id is not null then 'true' else 'false' end as validId
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder,(
Select Distinct a.id From collection_db a
Where a.id = 4) valid1
WHERE col1.id = 4
AND col2.category = 10
AND lend.memId = 1
You may have to do a preparatory query checking for a valid memId:
$theQuery = "SELECT DISTINCT memId FROM lendings WHERE memId = 1"
Then test it here:
if (mysql_num_rows(mysql_query($theQuery)) <= 0) { /* No memId exists */ }
else { /* Do big query here */ }
You can use a tableA LEFT JOIN tableB, which will return results for the tableA even if tableB has no matches and will return NULL values for those in tableB.
Unfortunately, I can't quite figure out where you need LEFT JOINS, but probably you want them in both places.
You also might have to reorder the tables if it is the first table that should be on the right side of a LEFT JOIN. You could use a RIGHT JOIN but it is less readable to me.
maybe you should try "left join" if col1 do not have too much data,or do the query step by step
I don't know if these are "complex queries" by defn, but they look very complex to a noob like me.
So I have a query here that will get the latest chart of customer_id=5:
$query = "SELECT c.Chart_ID, c.Chart_Notes
FROM tblchart AS c WHERE c.Customer_ID=5
ORDER BY c.Last_Edited ASC LIMIT 1";
But I have to relate it to another table that uses the Chart_ID as foreign key. How can I get the data from the tblcontent using tblchart.Chart_ID=tblcontent.Chart_ID? I couldn't just add that as:
$query = "SELECT c.Chart_ID, c.Chart_Notes, d.Content_Desc, d.Content_Title
FROM tblchart AS c, tblcontent AS d
WHERE c.Customer_ID=5 AND c.Chart_ID=d.Chart_ID
ORDER BY c.Last_Edited DESC LIMIT 1";
can I? As that would limit the search to just one...the use of LIMIT 1 is just to get the latest, but for the subsequent query (extended query), I am expecting multiple results extracted from tblcontent in addition to the first query I posted. A join, maybe, or union, or a complex query, but how? Please, can anyone help me? Thanks.
SELECT a.Chart_ID, a.Chart_Notes, c.Content_Desc, c.Content_Title
FROM tblChart a
INNER JOIN
(
SELECT Chart_ID, MAX(Last_edited) maxEdited
FROM tblChart
GROUP BY Chart_ID
) b ON a.Chart_ID = b.Chart_ID AND
a.Last_Edited = b.maxEdited
INNER JOIN tblcontent c
ON a.Chart_ID = c.Chart_ID
WHERE a.Customer_ID=5
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UNION query with codeigniter's active record pattern
I have the following code:
$language_id=$this->get_language_id($language_code);
$english_id=$this->get_language_id('en');
$query="SELECT e.label_value, t.user_id, t.votes, t.approved, t.language_value FROM labels e left outer join labels t on e.label_value=t.label_value WHERE e.language=$english_id and t.language=$language_id and (t.approved=1 or t.user_id=$user_id) and e.label_value in (select distinct label_value from labels WHERE language=$english_id order by label_value limit $start_index, 30) order by e.label_value, t.votes";
$query=$this->db->query($query);
$data=$query->result_array();
But I have got the following error:
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
So, I need to do the folowoing part "select distinct label_value from labels WHERE language=$english_id order by label_value offset $start limit 30" in another query. Please, help me, how can I do it using CodeIgniter?
UPDATE:
There are is table labels
(label_value, language_value, language) - PK,
user_id,
timestamp,
approved,
votes
and I need to get all queries from this table (for example, it's name is t and e) with labels t.label_value, e.label_value (is exists), e.user_id, e.votes, e.timestamp where t.label_value=e.label_value(same label), t.language=45 (english language), e.language=24 (my language) and (e.user_id=121234 or e.approved=1). But I need all entries, and if (t.label_value!=e.label_value) I need to get this entry with NULL fields.
This is a limitation of MySQL and not PHP or CI. In order to get around it, you need to wrap your sub query in an aliased sub query so it becomes a derived table:
$language_id = $this->get_language_id($language_code);
$english_id = $this->get_language_id('en');
$query = "
SELECT e.label_value, t.user_id, t.votes, t.approved, t.language_value
FROM labels e
LEFT OUTER JOIN labels t on e.label_value=t.label_value
WHERE
e.language = $english_id
AND t.language = $language_id
AND (t.approved = 1 OR t.user_id = $user_id)
AND e.label_value IN (
SELECT label_value
FROM (
SELECT DISTINCT label_value
FROM labels
WHERE language = $english_id
ORDER BY label_value
LIMIT $start_index, 30
) i
)
ORDER BY e.label_value, t.votes
";
$query = $this->db->query($query);
$data = $query->result_array();
I think that will work, let me know if it doesn't and I will take another look at it.
EDIT
I'm having a little difficulty working out exactly what you are trying to do, but I think it might be something more like this:
SELECT t.label_value, t.user_id, t.votes, t.approved, t.language_value
FROM (
SELECT DISTINCT label_value
FROM labels
WHERE language = $english_id
) e
LEFT JOIN labels t ON e.label_value = t.label_value
WHERE
t.language = $language_id
AND (t.approved = 1 OR t.user_id = $user_id)
ORDER BY t.label_value, t.votes
LIMIT $start_index, 30
If this is still not correct, please show some example rows, and the result set you would like to retrieve from those rows.