i dont know why i am getting this issue. i have set the datatype to ENUM. when i set its valye 'Y','N'
then its working fine for one field. Now i have another field named type ( ENUM) = '0','1','2'
Now i want to change/update its value then its not working.
here below i have mentioned my query
$BiddersItem = $this->paginate("BiddersItem", array_merge(array("BiddersItem.type"=>"2"), $filter));
that is the piece of the code for the update. i hope you can understand this.
i have set my debug on so check the query
SELECT COUNT(*) AS `count` FROM `event_mang_db`.`bidder_item_master` AS `BiddersItem` LEFT JOIN `event_mang_db`.`item_master` AS `Item` ON (`BiddersItem`.`item_id` = `Item`.`item_id`) LEFT JOIN `event_mang_db`.`atendee_master` AS `Attendee` ON (`BiddersItem`.`atendee_id` = `Attendee`.`atendee_id`) LEFT JOIN `event_mang_db`.`event_master` AS `Event` ON (`BiddersItem`.`event_id` = `Event`.`event_id`) WHERE **`BiddersItem`.`type` = 2** AND `BiddersItem`.`event_id` = 2
issue is that when you set BiddersItem.type` = '2' then its work fine.
suggest me the solution
thanks in advance
Related
Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.
I've posted yesterday an issue with an mySQL update syntax on CodeIgniter in here:
CodeIgniter - MySQL Error 1064 (Update table1 inner join table2(...))
But now after I solved that problem, another one come up. Now the update query doesn't know the new created table. But if I change to a select statement, it works smoothly.
For that reason I've decided to post the full script.
Code:
<?php
$this->load->database();
$query_tbaux='CREATE TABLE IF NOT EXISTS STUDY_LIST_AUX AS (
SELECT DISTINCT p.pat_id, p.pat_custom1 age, p.pat_name,
p.pat_sex, s.study_iuid, p.pat_birthdate, s.accession_no,
s.study_datetime date_s, s.study_desc, s.mods_in_study, s.pk,
c.institution, s.study_block, s.study_urgent,
\'0000-00-00 00:00:00\' AS \'report_date\', \'{null}\' AS \'report_status\',
s.study_tipo,
s.study_src,
s.study_consulta
FROM study s
INNER JOIN patient p ON s.patient_fk = p.pk
INNER JOIN series c ON c.study_fk = s.pk
INNER JOIN rel_users_hosp u ON u.hosp_id = c.institution
WHERE s.study_datetime >= \'2015-04-26 00:00:00\'
AND s.study_datetime <= \'2015-04-30 23:59:59\'
AND s.study_iuid IS NOT NULL
AND u.user_id = \'admin\'
)';
if ($this->db->query($query_tbaux))
{
echo "Q True!<br><br>";
$data = array(
'STUDY_LIST_AUX.report_date' => "DATE_FORMAT(study_report.report_date,'%Y-%m-%d %h:%i:%s')",
'STUDY_LIST_AUX.report_status' => 'study_report.report_status',
);
$this->db->update('STUDY_LIST_AUX, study_report', $data, array('STUDY_LIST_AUX.study_iuid'=>'study_report.study_iuid'));
}
else
{
echo "Q False<br><br>";
};
?>
Display/Error:
Q True!
A Database Error Occurred
Error Number: 1146
Table 'pacsdb.STUDY_LIST_AUX,' doesn't exist
UPDATE STUDY_LIST_AUX, study_report SET
STUDY_LIST_AUX.report_date =
'DATE_FORMAT(study_report.report_date,\'%Y-%m-%d %h:%i:%s\')',
STUDY_LIST_AUX.report_status = 'study_report.report_status' WHERE
STUDY_LIST_AUX.study_iuid = 'study_report.study_iuid'
I've checked phpmyadmin after refresh the page and the table really exists and it contains the data from the select statement.Can you please tell me what mistake I did?
This may cause you real problem.Remove ,study_report
after your table name.Try it
$this->db-
>update('STUDY_LIST_AUX',
$data,
array('STUDY_LIST_AUX.study_iuid'=>
'study_report.study_iuid'));
Pay closer attention.
Table 'pacsdb.STUDY_LIST_AUX,'
See that comma at the end? That should not be there. The error message is right.
The solution was more simple than it looks.
$query_update = "UPDATE STUDY_LIST_AUX
INNER JOIN study_report
SET STUDY_LIST_AUX.report_date = DATE_FORMAT(study_report.report_date,'%Y-%m-%d %h:%i:%s'), STUDY_LIST_AUX.report_status = study_report.report_status
WHERE STUDY_LIST_AUX.study_iuid = study_report.study_iuid";
$this->db->query($query_update);
It seems to be a issue with the CodeIgnigter Framework on the update integrated function ($this->db->update(...)). It doesn't work with sub queries, and the best solution goes for using the normal query execution function ($this->db->update($query))
Anyway, thanks for the help ;) All your answers were very helpful in a way to get to this solution.
with this code:
$result = mysql_query("
UPDATE skroutz SET
Image_Link=(
SELECT file_url
FROM bonnie_virtuemart_medias
INNER JOIN bonnie_virtuemart_product_medias
ON bonnie_virtuemart_medias. virtuemart_media_id =bonnie_virtuemart_product_medias. virtuemart_media_id
WHERE virtuemart_product_id=Unique_ID LIMIT 1)",$db);
I get a path of the form
/images/stories/virtuemart/product/1200x1000.gif
Get it from table in my database that already exists and the transfer to another table with UPDATE
Trying to add a static string in front of the name but I can not.
Specifically, I want the new table to convey stored as:
http://www.example.com/images/stories/virtuemart/product/1200x1000.gif
That adds to data in front the: www.example.com
Can someone help?
What I tried without success:
$result = mysql_query("
UPDATE skroutz
SET Image_Link=('www.example.com'
SELECT file_url
FROM bonnie_virtuemart_medias
INNER JOIN bonnie_virtuemart_product_medias
ON bonnie_virtuemart_medias. virtuemart_media_id =bonnie_virtuemart_product_medias. virtuemart_media_id
WHERE virtuemart_product_id=Unique_ID LIMIT 1)",$db);
You need to use the CONCAT function:
$result = mysql_query("
UPDATE skroutz
SET Image_Link=(
SELECT CONCAT('www.example.com', file_url)
FROM bonnie_virtuemart_medias
INNER JOIN bonnie_virtuemart_product_medias
ON bonnie_virtuemart_medias. virtuemart_media_id = bonnie_virtuemart_product_medias. virtuemart_media_id
WHERE virtuemart_product_id=Unique_ID LIMIT 1)",$db);
Since I made update of your question it is obviously that you are trying something strange.
If I got your goal correctly you can try something like:
SET Image_Link=CONCAT('www.example.com',(
SELECT file_url
...
))",$db);
I am creating a multisite CMS with codeigniter and am having trouble with a query for navigation links. Each page can have meta values that override the defaults (location_id = 0) with ones specific to the site if set. I have two tables cms_pages and cms_page_meta storing the information. Below is the relevant data
Table cms_pages
page_id
Table cms_page_meta
meta_id
location_id
page_id
tag_name
tag_value
I would like to do this in one call if feasible.
This is the call I was using, it causes pages to be listed twice if a title value exists for all locations (location_id = 0) and a specific location (location_id = x) where I just want the specific location if it exists otherwise the generic, otherwise NULL
SELECT cms_page.*, cms_page_meta.tag_value
FROM cms_pages
LEFT JOIN cms_page_meta ON cms_page_meta.page_id = cms_pages.page_id
WHERE cms_page_meta.location_id IN (0, x)
ORDER BY cms_pages.page_order`
I'm pretty sure I should to use coalesce but just can't figure out how to implement it. Am I trying to do too much at once? Should I split the calls and process with PHP array_merge? I'd like to avoid that as I can cache the call and delete/regenerate the cache whenever I make a change to the pages or metadata. The server gets ~10,000 hits a day so speed is helpful but not necessary yet...
I changed the above query so it only grabs generic data for right now. Any help is greatly appreciated!
I think you should use something like this:
SELECT
cms_page.*,
COALESCE(cms_pm2.meta_id, cms_pm1.meta_id) as meta_id,
COALESCE(cms_pm2.tag_name, cms_pm1.tag_name) as tag_name,
COALESCE(cms_pm2.tag_value, cms_pm1.tag_value) as tag_value
FROM
cms_pages LEFT JOIN cms_page_meta cms_pm1
ON cms_pm1.page_id = cms_pages.page_id
AND cms_pm1.location_id = 0
LEFT JOIN cms_page_meta cms_pm2
ON cms_pm2.page_id = cms_pages.page_id
AND cms_pm2.location_id = x
ORDER BY cms_pages.page_order
I think I found a solution
SELECT cms_pages.*, coalesce(s.tag_value, g.tag_value, 'Coming Soon!') as tag_value
from cms_pages
left join cms_page_meta s on ad_pages.page_id = s.page_id and s.tag_name = "title" and s.location_id = 255
left join cms_page_meta g on ad_pages.page_id = g.page_id and g.tag_name = "title" and g.location_id = 0
WHERE `cms_pages`.`page_group_id` = '5'
AND `page_type` = 1
ORDER BY `cms_pages`.`page_order`
Is this good performance-wise?
I have an issue getting data from three tables, which I want to return using one query.
I've done this before using a query something like this one:
$query = mysql_query("SELECT
maintable.`id`,
maintable.`somedata`,
maintable.`subtable1_id`,
subtable1.`somedata`,
subtable1.`subtable2_id`,
subtable2.`somedata`
FROM
`maintable` maintable,
`subtable1` subtable1,
`subtable2` subtable2
WHERE
maintable.`somedata` = 'some_search' AND
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
")or die(mysql_error());
The problem this time is that the extra details might not actually apply. I need to return all results that match some_search in maintable, even if there is no subtable1_id specified.
I need something that will go along the lines of
WHERE
maintable.`somedata` = 'some_search'
AND IF maintable.`subtable1_id` IS NOT NULL (
WHERE subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
)
As you will probably guess, I am not an advanced mysql user! Try as I might, I cannot get the syntax right, and I have had no luck searching for solutions on the web.
Any help much appreciated!
It seems like the basic distinction you're looking for is between an INNER JOIN and a LEFT JOIN in MySQL.
An INNER JOIN will require a reference between the two tables. There must be a match on both sides for the row to be returned.
A LEFT JOIN will return matches in both rows, like an INNER, but it will also returns rows from your primary table even if no rows match in your secondary tables -- their fields will be NULL.
You can find example syntax in the docs.
If I got this right, you need to use MySQL LEFT JOIN. Try this:
SELECT
m.id,
m.somedata,
m.subtable1_id,
s1.somedata,
s1.subtable2_id,
s2.somedata
FROM
maintable m
LEFT JOIN
subtable1 s1
ON
m.subtable1_id = s1.id
LEFT JOIN
subtable2 s2
ON
s1.subtable2_id = s2.id
WHERE
m.somedata = 'some search'
This will return the data of maintable even if there's no equivalent data in subtable1 or 2 OR data of maintable and subtable1 if there's no equivalent data in subtable2.
How about this:
WHERE
maintable.`somedata` = 'some_search'
AND (maintable.`subtable1_id` IS NULL OR (
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id` )
)
Keep in mind that this will result in a cross product of subtable1 and subtable2 with maintable when subtable1_id is NULL.