how to use isset in yii criteria - php

is there anyway to use isset in yii criteria?
i have two tables named equipment and supply.. They are pretty much the same except that equipment has this field named stock_no. Now I have a search function to show equipment or supply records depending on a dropdown.
Basically what I want is to use if isset to check if *stack_no* exists to prove that it is from equipment table.
if($this->itemType=="Equipment"){
$criteria->isset('stock_no', true ); // what may be the correct way to check if this column is existing?
}
if($this->itemType=="Supply"){
}

You can exequte a this query.
SELECT count(*) exist FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'yourSchema'
AND TABLE_NAME = 'yourTable'
AND COLUMN_NAME = 'stock_no'
This return 1 or 0 if exist this column on the table specified.

Related

How to set WHERE conditions if the IF() function returns FALSE and END query if it returns TRUE in mysql

I want to use mysql to check if a value exists in MySQL Database. If the value exists, I want to do nothing (I don't want to fetch any data). If it does not exist, then I want to set some where conditions.
So this is what I have so far, but its not right. because I still get data fetched, if no where condition is set
SELECT *,
CASE
WHEN ( table_name.record = 'inputrecord')
THEN
//Do nothing because it is found already
ELSE
// since inputrecord does not exist, we will start looking for 'id'
( WHERE table_name.id = '123')
END
FROM table_name
NOTE: In the above example, I have written WHEN ( table_name.record = 'inputrecord') and not WHEN ( table_name.record != 'inputrecord'). This is because I want to only continue the query if the data is not in the table.
Perhaps its better to use the IF function with the EXISTS function, but I am not sure how to do it.
Any help would be great. Right now I get errors
This is not how SQL works.
SQL always returns a collection of rows. It may be empty, sometimes rows may contain NULLs. You have to work out your conditions to filter the collection.
A generic example: say, we have a table of cars with columns (model, color, year), and you would like to find something about the cars in your table:
(* Find all red cars *)
select * from cars where color = 'red'
(* Find all red cars from 1985 *)
select * from cars where color = 'red' and year = 1985
(* Find all colors which exist both in 1990 and 2000 *)
select distinct color from cars A where year=1990 and exists (select 1 from cars B where year=2000 and B.color = A.color)
Please tell exactly what you are trying to achieve
EDIT: this should do it
(* select a record cars = 'Audi' only if cars = 'BMW' is not found in the whole table. otherwise. I do not want to select Audi even if it exists *)
select * from cars where model = 'Audi' and not exists (select 1 from cars where model = 'BMW')
here, IF there are BMWs in your table you'll get 0 rows, otherwise a list of Audis
select *
from table_name
where case when table_name.record <> 'inputrecord'
then table_name.id = '123'
else 1=1
end
you can apply the given condition in your code
when input condition does not match then apply your filter condition.
when input condition match(else case )then retrieve your desired result.
Try This...
SELECT *
CASE
WHEN table_name.record = 'inputrecord' THEN 'Unspecified'
WHEN table_name.id = '123' THEN 'table by id'
END
FROM table_name;

SQL query to find the table name

I have 5 different tables in my dB with the structure Name|Price|Id..
I have a unique price and Id entry combination.
Using these 2, what could be the possible SQL query to fetch the name of the table in which this entry is present?
I need to fetch the name of this table in order to update the value of Price.
You really should normalise your database properly and use a single table, but if you really need a kludge then:
SELECT name, brand, id, 'Tea' as tablename
FROM TableTea
WHERE brand = 'abc'
AND id = 100
UNION
SELECT name, brand, id, 'Coffee' as tablename
FROM TableCoffee
WHERE brand = 'abc'
AND id = 100
UNION
SELECT name, brand, id, 'Chocolate' as tablename
FROM TableChocolate
WHERE brand = 'abc'
AND id = 100
And you'll have to change it if you ever add new products
if your DBMS is MySQL you can use this Query:
SELECT Result.TABLE_NAME FROM (
SELECT TABLE_NAME,COLUMN_NAME,COUNT(*) AS QTA FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'NAME_DB'
and COLUMN_NAME IN ('Name','Price','Id')
GROUP BY TABLE_NAME
) as Result
WHERE Result.QTA = 3
i have already tried to do without php
Replace NAME_DB with your Database.
You would first need to know the tables in which it's possible to have the entry. Use a loop to iterate over those tables and run the query, each time returning a result set and testing if records exist in the result set. This will tell you what tables have the entry.
General Approach:
$array = array("table1", "table2", "table3", "table4", "table5");
foreach($array as $table) {
//build your query using the table name
$query = "SELECT something FROM " . $table;
//exec your query against your db and return results
//test if records exist in result set. If true, you know the table name based on the loop iteration ($table).
}

mysql query for selecting data in multiple columns

I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.

exclude certain items from fields in mysql

lets say you want to get all the columns of a table, but exclude ones that have for example _images in the name without having to do
mysql_query("SHOW COLUMNS FROM " . $table. " WHERE Field NOT IN ('project_images_1' .etc)")
is this possible?
field example:
header
bodytext
project_images_1 (exclude)
project_images_2 (exclude)
Yes, this is possible. I could do it with the LIKE operator:
SHOW COLUMNS FROM $table WHERE NOT Field LIKE '%_images%';
Or if you need more control, this might be helpful:
$query = <<<SQL
SHOW COLUMNS FROM $table WHERE Field NOT IN (
SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = $table
AND column_name LIKE 'project_images_%'
);
SQL;
This works by using the information schema tables (maybe that's what you look for anyway) within a so called subquery.
Related:
How do I use SHOW COLUMNS as a valid data source

Is it impossible to add a column in sql specifically before another column?

I am trying to add a column to my table before another column using BEFORE, I can't use AFTER because the column names before aren't a constant. This is what I have:
ALTER TABLE testfyf ADD intake_10_2013 VARCHAR(20) NOT NULL BEFORE url;
Everything up until BEFORE is working.
Before anyone says anything about this, I know adding columns this way is not a good idea, but it is what my boss wants.
Does anyone know how I can achieve this? I have found plenty of examples that say this should work but it doesn't.
MySQL (if this is mysql - you just tagged it sql generically) does not support a BEFORE keyword. You will have to find out the name of the previous column and add it with AFTER.
You can use this statement to list the column names and determine programmatically with PHP after which one your new one should be placed.
SHOW COLUMNS FROM tablename;
Here's a function that should return the column before 'url'. I didn't test it but I think it will do the job.
// Queries the table column information and returns the column name just before `url`
function get_column_before_url() {
$result = mysql_query("SHOW COLUMNS FROM testfyf");
while ($row = mysql_fetch_array($result)) {
if ($row['Field'] == "url") {
// you should clean up your mysql resources before returning
mysql_free_result($result);
return $lastcol;
}
// Store the column name to return if the next one turns out to be `url`
else $lastcol = $row['Field'];
}
}
Now you can do:
$column_before = get_column_before_url();
$sql = "ALTER TABLE testfyf ADD intake_10_2013 VARCHAR(20) NOT NULL AFTER $column_before;";
$result = mysql_query($sql);
if (!$result) echo $mysql_error();
You could use the information_schema to get the columns of the table in order:
SELECT c.COLUMN_NAME
, #n := #n + 1 AS column_id
FROM
( SELECT COLUMN_NAME
FROM information_schema.columns
WHERE TABLE_SCHEMA = 'yourDatabase'
AND TABLE_NAME = 'yourTable'
) AS c
CROSS JOIN
( SELECT #n := 0 ) AS v
If you join the above to itself, you could get the name of the previous column and use ADD COLUMN with AFTER.

Categories