How to Joing two tables in codeigniter with where clause - php

Please any one cal help me
My query is following
Column 'id' in where clause is ambiguous
SELECT u_profile.*, u_user.* FROM u_profile JOIN u_user ON u_profile.id = u_user.id WHERE id = '1' AND u_profile.id = '1'
from where id = 1 is coming I don't know please can help me some one.

You need to indicate the table name in your where clause, because SQL does not know what id is until you indicate the table to which it belongs.
Assuming you want to find both user_id and profile_id :
SELECT u_profile.*, u_user.* FROM u_profile
JOIN u_user ON u_profile.id = u_user.id
WHERE u.user_id = '1' AND u_profile.id = '1';

You can use like that in CodeIgniter For INNER JOIN:
$this->db->select();
$this->db->from('u_profile')->join('u_user', 'u_profile.id = u_user.id');
$this->db->where('u_user.id',1);
$this->db->where('u_profile.id',1);
For LEFT JOIN use this:
$this->db->select();
$this->db->from('u_profile')->join('u_user', 'u_profile.id = u_user.id','left');
$this->db->where('u_user.id',1);
$this->db->where('u_profile.id',1);
And Column 'id' in where clause is ambiguous means, you have two column with same in both table like id, so you need to use as u_user.id AND u_profile.id.

Related

Update MySQL table from view with aliases

I'm trying to update rows in MySQL from SQL view created with aliases. I do not know if this even is possible.
The original SQL looks like below. This I have saved as a view "ProductVersion".
SELECT e.entity_id AS id,
v1.value AS name,
e.sku,
d1.value AS version
FROM mguu_catalog_product_entity e
LEFT JOIN mguu_catalog_product_entity_varchar v1 ON e.entity_id = v1.entity_id
AND v1.store_id = 0
AND v1.attribute_id =
(SELECT attribute_id
FROM mguu_eav_attribute
WHERE attribute_code = 'name'
AND entity_type_id =
(SELECT entity_type_id
FROM mguu_eav_entity_type
WHERE entity_type_code = 'catalog_product'))
LEFT JOIN mguu_catalog_product_entity_varchar d1 ON e.entity_id = d1.entity_id
AND d1.attribute_id = 171;
Now I would like to perform a MySQL UPDATE from that view, so I tried:
mysqli_query($db, "UPDATE ProductVersion SET version = '123123123' WHERE sku = '1003'");
This does not return any error - however it does not update though. I have also tried like this, but this creates an error:
mysqli_query($db, "UPDATE ProductVersion SET d1.value = '123123123' WHERE e.sku = '1003'");
Is it possible to do an MySQL UPDATE from a view and if not, how would it be done the easiest from the original SQL?
Thanks in advance.
I don't know if that solve the problem but I think you can simplify your View something like this (less useless select statement) :
SELECT e.entity_id AS id,
v1.value AS name,
e.sku as sku,
d1.value AS version
FROM mguu_catalog_product_entity e
LEFT JOIN mguu_catalog_product_entity_varchar v1 ON (e.entity_id = v1.entity_id)
LEFT JOIN mguu_catalog_product_entity_varchar d1 ON (e.entity_id = d1.entity_id)
LEFT JOIN mguu_eav_attribute AS mea ON (v1.attribute_id = mea.attribute_id)
LEFT JOIN mguu_eav_entity_type AS meet ON (meet.entity_type_code = 'catalog_product')
WHERE v1.store_id = 0
AND mea.attribute_code = 'name'
AND mea.entity_type_id = meet.entity_type_id
AND d1.attribute_id = 171;
In MySql documentation :
UPDATE: The table or tables to be updated in an UPDATE statement may be view references that are merged. If a view is a join view, at least one component of the view must be updatable (this differs from INSERT).
In a multiple-table UPDATE statement, the updated table references of the statement must be base tables or updatable view references. Nonupdated table references may be materialized views or derived tables.
https://dev.mysql.com/doc/refman/8.0/en/view-updatability.html

Null result while use and operator in MYSQL IN

This is my query to get filter data from user table where category will fetch from another table.
SELECT * FROM jobfia_users
WHERE country_id='4'
and user_id IN (SELECT worker_id
FROM jobfia_worker_skills
WHERE skill_id = '42'
)
This is not giving any error, but not return any row also.
while there are lots of records are available in table using this filter.
Can any one help please ?
Additionally to the quotes surrounding your INT ids, your query will be better expressed like this :
SELECT u.*
FROM jobfia_users u
INNER JOIN jobfia_worker_skills ws
ON ws.worker_id=u.user_id AND ws.skill_id = 42
WHERE u.country_id=4
If your country_id and skill_id are int type, remove ' around values.
SELECT * FROM jobfia_users
WHERE country_id=4
and user_id IN (SELECT worker_id
FROM jobfia_worker_skills
WHERE skill_id = 42
)
Tested your code with and without '' and it works. Make sure you have data and you did not misspell some column name.
Maybe you have collision of some column names. Try to use this syntax:
\`table_name\`.\`column_name\`
Code:
SELECT *
FROM `jobfia_users`
WHERE `jobfia_users`.`country_id`='4'
AND `jobfia_users`.`user_id` IN (SELECT `jobfia_worker_skills`.`worker_id`
FROM `jobfia_worker_skills`
WHERE `jobfia_worker_skills`.`skill_id` = '42')

SQL Query: do I need two queries or can I use a nested subquery

This question may have been asked before but I don't really know what verbiage to search with.
I have a mysql DB that has a table with 3 columns [ID, fieldName and fieldValue] that is used to describe attributes of objects in another table. The ID field stores the foreign key of object in the other table and the fieldName and fieldValue store things like title, description, file size and summary.
I am trying to write a query that returns rows where a fieldName and fieldValue pair match known values and the returned row ID has a another distinct fieldValue in another row. Right now I am accomplishing it with two queries and an if statement. Here is the sudo code:
$result = SELECT * FROM table_a WHERE fieldName = 'title' and fieldValue = 'someTitle'
$test = SELECT * FROM table_a WHERE fieldValue = 'someValue' and id = '{$result['id']}'
if ($test) {
/* Result Found */
}
You can self-join the table:
SELECT * FROM table_a AS s1
JOIN table_a AS s2 USING (id)
WHERE
s1.fieldName = 'Title' AND s1.fieldValue = 'someTitle'
AND s2.fieldValue = 'someValue'
What you said translated in sql would be:
SELECT b.*
FROM table_a a
INNER JOIN table_a b ON a.id = b.id
WHERE a.fieldName = 'title'
AND a.fieldValue = 'someTitle'
AND a.fieldValue <> b.fieldValue
This gets you the rows in table_a that have the same id as the row with you predefined values, but with a different fieldValue. This assumes that id is not the primary key, otherwise there will not be another row with the same id, but it looks in your question that this isn't the case. (If you want to check for a specific value you can do: AND b.fieldValue = 'someValue' in the last line)

Mysql Complex Join Query Issue

I have 5 mysql tables as described below.
clinics table
id
name
d_location_subscription table
id
clinic_id
t_id //t_id will contain a foreign key of d_cities, d_states or d_countries table
type "country" "state" "city"
d_countries table
id
name
code
d_states table
id
d_country_id
name
code
d_city table
id
d_state_id
name
code
d_location_subscription table is used to record clinic's subscription for a location(it may be a city, state or country). I'm expecting to get all subscribed cities for a specific
clinic using d_location_subscription table.
For example, if clinic A is subscribed to Texas state, I should be able to get all city ids for clinic A.
I created following sql query, it looks ugly but generate a close result what i want to achieve.
select
`d`.`id` AS `clinic_id`,
if((`dct`.`id` is not null),`dct`.`id`,if((`dct1`.`id` is not null),`dct1`.`id`,`dct2`.`id`)) AS `d_city_id`
from ((((((((
`d_location_subscriptions` `dls`
join `clinics` `d`
on((`d`.`id` = `dls`.`clinic_id`)))
left join `d_countries` `dc`
on(((`dc`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'country'))))
left join `d_states` `ds`
on((`ds`.`d_country_id` = `dc`.`id`)))
left join `d_cities` `dct2`
on((`dct2`.`d_state_id` = `ds`.`id`)))
left join `d_states` `ds1`
on(((`ds1`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'state'))))
left join `d_cities` `dct`
on((`dct`.`d_state_id` = `ds1`.`id`)))
left join `d_cities` `dct1`
on(((`dct1`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'city'))))
)
when there is record with type "country" in d_location_subscription table, I receive following result. total number of records returned are equal to the number of d_states table records.
How should I get rid of those Null values by changing above query?
And please advice me if this is the correct way to acheive similar functionality. Thanks in advance :)
The quickest, dirtiest way to achieve what you want is just to append this where condition to your query:
WHERE d_city_id is not null
but you might prefer to rework your query and decide where you really need LEFT joins and not INNER joins
the IF() computed column is in essence what STT LCU was trying to offer, but you can't use that directly in the where for some reason.
I've rewritten your query, but with different aliases to better follow the origination of the tables / relationships to get the data. In the end, I've added a where to test for ANY ONE of the "ID" values as NOT NULL. If they are ALL Null, the record should be excluded..
select
d.id AS clinic_id,
if(CityViaState.id is not null, CityViaState.id,
if( ByCity.id is not null, ByCity.id, CityViaCountry.id )) AS d_city_id
from
d_location_subscriptions dls
join clinics d
ON dls.clinic_id = d.id
left join d_countries ByCountry
ON dls.t_id = ByCountry.id
and dls.type = 'country'
left join d_states StateViaCountry
ON ByCountry.id = StateViaCountry.d_country_id
left join d_cities CityViaCountry
ON StateViaCountry.id = CityViaCountry.d_state_id
left join d_states ByState
ON dls.t_id = ByState.id
and dls.type = 'state'
left join d_cities CityViaState
ON ByState.id = CityViaState.d_state_id
left join d_cities ByCity
ON dls.t_id = ByCity.id
and dls.type = 'city'
where
CityViaState.id is not null
OR ByCity.id is not null
OR CityViaCountry.id is not null

PHP / MySQL - Constructing a Query

Im trying to put together a SQL query and its got me all confused, i have written out in normal language what i need, i cant seem to get it.
select * from introles where introle = $key
then check the table 'availability' for the user_id taken from the introles table
then out of those results, check that $_POST['date'] is not equal to the date in the 'availability' table
Any help would be amazing :)
EDIT: The table structure is as follows
Table introles has the following
id
user_id
introle
Table availability has the following
id
user_id
date
can you try
'$variable'
instead of
$variable
for all variables in sql query?
$query = "SELECT a.id AS aId, i.id AS iId, a.user_id, introle, date FROM availability AS a, introle AS i WHERE date != {$_POST['date']} AND a.user_id IN (SELECT user_id FROM introle WHERE introle = {$key})";

Categories