I have three tables that I want to be able to delete select rows from, if a given condition in one of the tables is true. I am using codeigniter.
My Table Names are: Register, Profiles and Profilesmember. All 3 tables have the column "Email" which I want to Join on. One table "Register" has a unique column "is_email_verfiied" - which is either "yes" or "no". If it is "no", I want to delete the column from table "register" as well as one of the other two tables "Profiles" and "Profilesmember" depending on which contains matching entires in the 'email' column.
Here is my model:
function databaseupdate ()
{
$verified = 'no';
$tables = array('register', 'profiles', 'profilesmember');
$this->db->from ('register');
$this->db->join('profiles', 'profiles.email = register.email');
$this->db->join('profilesmember', 'profilesmember.email = register.email');
$this->db->where('register.is_email_verified', $verified);
$this->db->delete($tables);
}
It deletes the correct entry from the table "Register", but does not from the second table Profilesmember or Profiles as the case may be.
The error message I get is
"Unknown column 'register.is_email_verified' in 'where clause'".
I checked and the column "is_email_verified" is spelled correctly, and of course does exist in table Register (for clarity, this column does not exist in the other 2 tables).
The last SQL query is
"DELETE FROM profiles WHERE register.is_email_verified = 'no' "
So I changed my approach and got rid of the JOIN idea - this works:
function databaseupdate ()
{
$verified = 'no';
$tables = array('register', 'profiles', 'profilesmember');
$this->db->select('email');
$this->db->from('register');
$this->db->where('is_email_verified', $verified);
$query = $this->db->get();
$email = $query->row()->email;
$this->db->where('email', $email);
$this->db->delete($tables);
}
Related
I am trying to fetch data from 2 tables with a join query. Here I have 2 columns in 2 tables with same column name.
This is my query:
public function get_all_expenses()
{
$this->db->select("*",'category.name as cat_name');
$this->db->from('expense');
$this->db->join('category','expense.cat_id = category.id');
$this->db->join('users','expense.user_id = users.id');
$query = $this->db->get();
return $query;
}
I can fetch data of 1 column of 1 table. But I can't fetch data of another column of another table. I am using CodeIgniter.
According to the CodeIgniter documentation the database select method accept a single argument. The correct syntax for the select is then:
$this->db->select('*, category.name as cat_name');
I have two related table and I have to display data from both trough a join query.
I have this GridView that is "working" when i have to do the search, so that's mean if I have to search into a field from the joined table, it works and shows the right result, but I am not able to show all the column from both table.
Here is the error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S02]: Base
table or view not found: 1051 Unknown table 'pratiche'. The SQL
statement executed was: SELECT pratiche.* FROM pratiche t LEFT
JOIN clienti ON id_cliente = clienti.id LIMIT 10
As you can see there is t after pratiche, so it fail the query.
What's the problem?
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->select = "pratiche.*,clienti.*";
$criteria->join='LEFT JOIN clienti ON id_cliente = clienti.id';
$criteria->compare('id_pratiche',$this->id_pratiche,true);
//this is the fild that should be shown(from the joined table)
$criteria->compare('codice_fiscale',$this->codice_fiscale,true);
$criteria->select = "pratiche.*";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
SOLVED!
Wrong way to set the criteria:
$criteria->select=array('id_pratiche','data_creazione','stato_pratica','nome','cognome');
I am adding a new column to my azure table. For ex., the table is called 'User' and the new column is called 'ComputationDate'. The 'User' table already exists with rows that do not have this new column 'ComputationDate'. I have a query over it as follows,
var usersDue = from user in Table.Query
where user.ComputationDate != <somedate>
select user;
I want this query to return all user rows that do not have ComputationDate set to 'somedate' and also user rows that do not have this new 'ComputationDate' column defined.
But the query doesn't return the latter set of users. It only returns rows that have 'ComputationDate' set and where the value is not equal to 'somedate'.
Is there any way to get the results I desire without having to get all users and filter it on the client?
It looks like you're trying to do a LINQ to SQL query.
This may serve your needs better:
var usersDue = from user in Table.Query
where user.ComputationDate != <somedate>
|| user.ComputationDate == null
select user;
I have two tables: Users and Groups
In my table "Users", there is a column called "ID" for all the user ids.
In my table "Groups" there is a column called "Participants", fields in this column are filled with all the user ids like this "PID_134,PID_489,PID_4784," - And there is a column "ID" that identifies a specific group.
Now what i want to do, i want to create a menu that shows all the users that are not yet in this particular group.
So i need to get all the user ids, that are not yet in the Participants column of a group with a particular ID.
It would be cool if there was a single mysql query for that - But any PHP + MySQL solutions are okay, too.
How does that work? Any guesses?
UPDATE:
i know, that's not code, but is there a way I could do something like this that would return me a list of all the users?
SELECT *
FROM users, groups
WHERE groups.participants NOT LIKE '%PID_'users.id'%' AND groups.id = 1;
Something like this. You just get rid of "PID_" part of ID.
SELECT * FROM [users] WHERE [id] NOT IN
(SELECT replace(id,'PID_','') FROM groups WHERE group_name='group1')
Group1 would be your variable - group id/name of menu that you've opened.
You can select from multiple tables as shown below:
SELECT * from users, groups WHERE users.id != groups.participants AND groups.id = 1;
This will list all users who are not in group id 1; A more elegant solution can be found by using joins, but this is simple and will do the trick.
I believe something like that should help:
SELECT * FROM users WHERE users.id NOT IN (SELECT groups.participants FROM groups)
But this works only if your DB is normalized. So for your case I see only PHP + MySQL solution. Not very elegant, but it does the job.
<?php
$participants_array = mysql_query("SELECT participants FROM groups");
$ids = array();
while ($participant = mysql_fetch_assoc($participants_array))
{
$id = explode(',', $participant['participant']);
foreach ($id as $instance)
{
if (!in_array($instance, $ids)) $ids[] = $instance;
}
}
$participants = implode(',', $ids);
$result = mysql_query("SELECT * FROM users WHERE id NOT IN ( $participants )");
But I highly recommend normalizing the database.
I have few MySQL tables which I need to get sum of the data from them. There in PHP I use a common code for all of these after I selected particular table I want from a drop down list.
$result3 = mysqli_query($con, "SHOW COLUMNS FROM ".$table_name." ");
if (!$result3) {
echo 'Could not run query: ' . mysqli_error($con);
exit;
}
if (mysqli_num_rows($result3) > 0) {
while ($row3 = mysqli_fetch_assoc($result3)) {
$topics[] = $row3['Field'];
}
}
when I get column names of the SQL table to the array $topic using that array I am getting the sum of each table column
for ($x=0; $x<sizeof($topics);$x++){
$for_sum= "SUM(".$topics[$x].")";
$new_array_sum[]=$for_sum;
}
$comma_separated = implode(",", $new_array_sum);
$sql=" SELECT ".$comma_separated."FROM $table_name
This works well and my problem is when I have columns in some tables that I don't need sum such as ID s what data type in SQL I should define the column to get sum as zero or meaningfull thing. I tried with VARCHAR and still it gives sum.
Check this
http://dev.mysql.com/doc/refman/5.6/en/type-conversion.html
MySQL converts the expression to numeric when the expression is evaluated in a numeric context