I am making a query which is not working. I want to get data where column of pic in is not empty in all_destination table, also I am using join in my query:
SELECT *
FROM `travels_detail`
INNER JOIN all_destination ON travels_detail.destination = all_destination.destination
WHERE all_destination.pic IS NOT NULL
whats wrong in my query?
You should check what is the error returned by MySQL. It will tell you what is the error.
You just want to remove the IS NOT NULL check and just have
SELECT *
FROM `travels_detail`
INNER JOIN all_destination ON travels_detail.destination = all_destination.destination
WHERE all_destination.pic;
This will only return records where all_destination.pic has been set.
try this:
SELECT td.some_attribute
FROM travels_detail as td
INNER JOIN all_destination as ad ON td.destination = ad.destination
WHERE ad.pic IS NOT NULL
You can try this.. It mostly works
SELECT * FROM `travels_detail` INNER JOIN all_destination ON travels_detail.destination = all_destination.destination WHERE all_destination.pic ! = ""
Related
In my controller :
I have function like this :
$this->admindata->examview($a,3);
In model, I just have function like this :
function examview($examid, $examtipe){
$this->db->select("exam_id");
$this->db->from("mainexam");
$query = $this->db->get()
return $query->result();
}
And i got error :
Column 'id_group' in field list is ambiguous
SELECT `mu`.`obli`, `mu`.`id_exam_question`, `p`.`id_question`, `question`, `type_question`, `m`.`id_gabungan`, `p_parent`, `id_group` FROM (`exam`, `exam` mu) LEFT JOIN `randomexam` c ON `mu`.`id_group`= `c`.`id_question_order` LEFT JOIN `question` p ON `p`.`id_question` = `c`.`id_question` LEFT JOIN `main` m ON `m`.`id_question` = `p`.`id_question` WHERE `mu`.`id_exam` = '10' GROUP BY `mu`.`id_exam_question` ORDER BY `question_type` asc, LIMIT 0
I don't even have JOIN in my function. And If I delete $this->admindata->examview($a,3), My error has gone. Codeigniter try to call other function I think.
Nah it's tripping up on something else, a query before this one is in need of fixing. Your query here would be like
SELECT exam_id FROM mainexam;
PHP & MYSQL: How to resolve ambiguous column names in JOIN operation?
Try to track down the query causing the error and apply bandages.
I have two databases and I am trying to compare two tables. My code does not seem to be working, not sure what I am doing wrong.
Here is the code.
<?php
include 'connection.php';
/*
* This code compares between two tables
*/
//SQL call
$getData = $connection->prepare("SELECT `CustomerCity` FROM `auth` LEFT JOIN `tb_data.cobs.city` WHERE `CustomerCity` = `tb_data.cobs.city` LIMIT 3");
$getData->execute();
$gotData = $getData->fetchAll(PDO::FETCH_ASSOC);
print_r($gotData);
In my database I have two tables, on is cobs, the other is tb_data. tb_data has a table called cobs and auth is a table within a database called d_data. Both of these tables have a city column. I need get every record in auth that has a city that matches in the cobs table.
That looks like the query is using a mixture of explicit join syntax with obsolescent syntax for the join using the WHERE clause for the join conditions.
If so try:-
SELECT CustomerCity
FROM auth
LEFT JOIN tb_data.cobs
ON auth.CustomerCity = cobs.city
LIMIT 3
Others have pointed out that your query is wrong, but have not provided a correct answer. This is what you are likely looking for:
SELECT `auth.CustomerCity` FROM `auth`
LEFT JOIN `tb_data.cobs` ON `tb_data.cobs.city` = `auth.CustomerCity`
LIMIT 3
Try this :
Select *.auth, *.cobs from auth join cobs on auth.city = cobs.city limit 3
The query is incorrect, you need to specify the link betweeen the tables auth and tb_data.cobs.city. For example:
SELECT
*
FROM
FOOTABLE FOO
LEFT JOIN
BARTABLE BAR ON BAR.FOO_ID FOO.ID = -- here goes the link between them
WHERE
...
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 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
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.