I'm really not sure what's going on at this point. I'm trying to create a view, and when I run the query below:
$view = oci_parse($conn, '
CREATE OR REPLACE VIEW
V_ORD_HISTORY
AS
SELECT
ARCUSTO.ID,
ORDERS.ID,
ORDERS.PONO,
ORDERS.ORDERNO,
ORDERS.DATE_TAKEN,
ORDERS.ARCUSTO_ID,
ORD_DETAIL.ORDERS_ID,
ORD_DETAIL.ID,
ORD_DETAIL.TOTAL_QTY_ORD,
ORD_DETAIL.CUMM_SHIPPED,
ORD_DETAIL.DISCOUNT,
ORD_DETAIL.UNIT_PRICE
FROM
ARCUSTO a,
ORDERS o,
ORD_DETAIL d,
ARINVT i
WHERE
a.ID = o.ARCUSTO_ID
AND
d.ORDERS_ID = o.ID
');
I get the error:
ORA-00904: "ORD_DETAIL"."UNIT_PRICE": invalid identifier
And if I try to delete ORD_DETAIL.UNIT_PRICE from the end of the select statement, then it tells me that ORD_DETAIL.DISCOUNT is now the invalid identifier. Whichever column is last in the list becomes the invalid identifier. If I remove the prefix to determine which table, then I get column ambiguously defined errors.
Any ideas??
EDIT 1
I've now changed the code to the following:
$view = oci_parse($conn, '
CREATE OR REPLACE VIEW
V_ORD_HISTORY
AS
SELECT
a.ID as CUSTOMER_ID,
o.ID as ORD_ID,
o.PONO,
o.ORDERNO,
o.DATE_TAKEN,
o.ARCUSTO_ID,
d.ORDERS_ID,
d.ID as DETAIL_ID,
d.TOTAL_QTY_ORD,
d.CUMM_SHIPPED,
d.DISCOUNT,
d.UNIT_PRICE
FROM
ARCUSTO a,
ORDERS o,
ORD_DETAIL d,
ARINVT i
WHERE
CUSTOMER_ID = o.ARCUSTO_ID
AND
ORD_ID = d.ORDERS_ID
');
But now it's saying that ORD_ID is an invalid identifier. This its the last line of the SQL statment.
How about trying :
CREATE OR REPLACE VIEW
V_ORD_HISTORY
AS
SELECT
a.ID as CUSTOMER_ID,
o.ID as ORD_ID,
o.PONO,
o.ORDERNO,
o.DATE_TAKEN,
o.ARCUSTO_ID,
d.ORDERS_ID,
d.ID as DETAIL_ID,
d.TOTAL_QTY_ORD,
d.CUMM_SHIPPED,
d.DISCOUNT,
d.UNIT_PRICE
FROM
ARCUSTO a,
ORDERS o,
ORD_DETAIL d,
ARINVT i
WHERE
a.id= o.ARCUSTO_ID
AND
o.id = d.ORDERS_ID
Related
I have the following PHP code that works perfectly ($qry_str is actually generated in the PHP):
$qry_str = <<<'QRY'
FIND_IN_SET('6-47', attributes)
AND FIND_IN_SET('4-176', attributes)
AND FIND_IN_SET('9-218', attributes)
QRY;
$pdo->query('DROP TEMPORARY TABLE IF EXISTS `temp_attr`');
$temp_sql = <<<"TEMP"
CREATE TEMPORARY TABLE IF NOT EXISTS `temp_attr` (
SELECT product_id, GROUP_CONCAT(CONCAT(group_id, '-', IF (custom_value != '', custom_value, value_id)) SEPARATOR ',') AS attributes
FROM `products_attributes`
GROUP BY `product_id`
HAVING $qry_str
);
TEMP;
$pdo->query($temp_sql);
$sql = "SELECT
m.recommended_price AS msrp,
m.purchase_price AS cost,
pp.USD AS regular_price,
pc.USD AS special_price,
pc.start_date AS start_date,
pc.end_date AS end_date,
pl.permalink AS permalink,
pi.name AS description,
m.sku AS sku,
m.default_category_id AS cat,
m.id AS prod_id
FROM `products` AS m
LEFT JOIN `products_prices` AS pp ON m.id = pp.product_id
LEFT JOIN `products_campaigns` AS pc ON m.id = pc.product_id
LEFT JOIN `permalinks` AS pl ON (m.id = pl.resource_id AND pl.resource = 'product')
LEFT JOIN `products_info` AS pi ON (m.id = pi.product_id)
LEFT JOIN `products_to_categories` AS ptc ON (m.id = ptc.product_id)
INNER JOIN `temp_attr` AS pa
WHERE ptc.category_id = :cat
AND m.status = 1
AND m.id = pa.product_id
LIMIT 55;
";
$data = $pdo->prepare($sql)
->bindValue('cat', 100)
->execute()
->fetchAll();
However, when I use a placeholder in the temp table code, i.e.
$temp_sql = <<<"TEMP"
CREATE TEMPORARY TABLE IF NOT EXISTS `temp_attr` (
SELECT product_id, GROUP_CONCAT(CONCAT(group_id, '-', IF (custom_value != '', custom_value, value_id)) SEPARATOR ',') AS attributes
FROM `products_attributes`
GROUP BY `product_id`
HAVING :qry_str
);
TEMP;
$sth = $pdo->prepare($temp_sql);
$sth->bindValue('qry_str', $qry_str, PDO::PARAM_STR);
$sth->execute();
I get the following error:
PHP Fatal error: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: 'FIND_IN_SET('6-47', attributes)
AND FIND_IN_SET('4-176', attributes)
AND FIND_IN_SET('9-218', attributes)
AND FIND_IN_SET(...'
There are no datetime columns in this table.
group_id and value_id are integer columns
Since the code works fine without the placeholder, I'm at a loss as to why the use of a placeholder breaks the code. The placeholder in the main SQL works properly.
PHP 8.0
https://dev.mysql.com/doc/refman/8.0/en/prepare.html explains:
Parameter markers can be used only where data values should appear, not for SQL keywords, identifiers, and so forth.
In your case, you're apparently trying to bind an expression with the FIND_IN_SET() function. You can't do that. All expressions and other SQL syntax must be fixed in the query at the time you prepare it. You can use a parameter only in a place where you would otherwise use a scalar literal value. That is, a quoted string or a numeric literal.
I'm trying to update multiple joined tables in codeigniter but it is giving the following error:
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.carro_id INNER JOIN a ON ac.atributo_id = a.id INNER JOIN
m ON c.`modelo_' at line 1
UPDATE `carro c, atributo_carro ac, atributos a, modelos m, marcas ma
INNER JOIN ac ON c`.`id = ac`.`carro_id INNER JOIN a ON
ac`.`atributo_id = a`.`id INNER JOIN m ON c`.`modelo_id = m`.`id INNER
JOIN ma ON m`.`marca_id =` `ma`.`id` SET `marcas`.`marca` = 'Opel',
`modelos`.`modelo` = 'AMmmmmm', `carro`.`kilometragem` = '12000',
`carro`.`cor` = 'Azul', `carro`.`ano` = '1992', `carro`.`descricao` =
'dfghjk', `carro`.`valor` = '1288.00', `atributos`.`atributo` =
'rapido' WHERE `carro`.`id` = '10'
This is my function to update.
public function update_carro_query($id, $data)
{
// print_r($data); die();
$query =
$this->db->set('marcas.marca', $data['marca']);
$this->db->set('modelos.modelo', $data['modelo']);
$this->db->set('carro.kilometragem', $data['kilometragem']);
$this->db->set('carro.cor', $data['cor']);
$this->db->set('carro.ano', $data['ano']);
$this->db->set('carro.descricao', $data['descricao']);
$this->db->set('carro.valor', $data['valor']);
$this->db->set('atributos.atributo', $data['atributo']);
$this->db->where('carro.id', $id);
$this->db->update('carro c, atributo_carro ac, atributos a, modelos m, marcas ma INNER JOIN ac ON c.id = ac.carro_id INNER JOIN a ON ac.atributo_id = a.id INNER JOIN m ON c.modelo_id = m.id INNER JOIN ma ON m.marca_id = ma.id');
return $query;
}
This is my database tables and relationships
The query is generated by the function. What can be causing the syntax error? Am i missing something?
The update() method only expects a table name as first parameter, not a complete statement including a join and more. So it'll wrap the first parameter into backticks. This leads to the sql syntax error you have.
Better use query() to generate your (not straightforward) update statement.
I'm working with a mysql query to select data from multiple tables using LEFT OUTER JOIN. Now i get the following error when i exequte the query:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'wg.werkbon_global_id = wk.werkbon_klant_globalid LEFT OUTER
JOIN users AS u' at line 16
Only the problem is that i can't find out what's wrong with my query.
PHP Query:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC;
$result = $db->loadAssoc($query);
I think my problem has something todo with left outer join but what?
You are missing the ON operator in your joins!
The correct syntax for a join is:
SELECT * FROM x LEFT JOIN y ON condition WHERE...
$query = "
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC";
$result = $db->loadAssoc($query);
Make sure there isn't missing quote
Problem soved thanks to arkascha
The fixed query is now:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg ON
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr ON
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC';
$result = $db->loadAssoc($query);
#fred i don't need to add quotes by column names. You only need to add quotes by string/blob values.
#johny my $db->Quote() function will add qoutes automaticly. I don't need to add them and put everything in quote's.
Thanks all for help.
I have a result set and need to be displayed as it is on the screen.But the problem is while displaying each row of the result set using echo command the order is getting changed.Can anyone say why this is happening and provide me a way to overcome it.Here are my actual and printed outputs.
Actual Result set:
JAIKE-ILENE-WACKI-MAZIE-REGLE-SBJ-KMMU
LVZ-HARTY-MUGZY-STW
MAZIE-SIXIE-SBJ-KMMU
PXT-LOUIE-GATBY-RAZER-BUZIE-JAIKE-ILENE-WACKI-MAZIE
SWANN-GATBY-RAZER-BUZIE-JAIKE-ILENE-WACKI-MAZIE
Output:
JAIKE-SBJ-ILENE-KMMU-WACKI-MAZIE-REGLE
MUGZY-STW-LVZ-HARTY
SBJ-KMMU-MAZIE-SIXIE
ILENE-GATBY-WACKI-RAZER-MAZIE-BUZIE-PXT-JAIKE-LOUIE
WACKI-RAZER-MAZIE-BUZIE-JAIKE-SWANN-ILENE-GATBY
Here is my code
$sql3="SELECT GROUP_CONCAT(l.fix_ident SEPARATOR '-') AS fix_seq,l.airport_ident,x.star_ident,x.transition_ident,
x.fix_ident from corept.std_star_leg l
JOIN
(SELECT DISTINCT c.airport_ident,c.star_ident,c.transition_ident,c.fix_ident
FROM corept.std_star_leg c
INNER JOIN
(SELECT star_ident,transition_ident,max(sequence_num) seq,route_type
FROM corept.std_star_leg
WHERE data_supplier='J'
AND airport_ident='KMMU'
GROUP BY star_ident,
transition_ident)b ON c.sequence_num=b.seq
AND c.star_ident=b.star_ident AND c.transition_ident=b.transition_ident
LEFT JOIN
(SELECT name,trans
FROM skyplan_deploy.deploy_stars
WHERE apt='KMMU'
AND name!=trans) d
ON d.name=c.star_ident
AND d.trans=c.fix_ident
WHERE c.data_supplier='J'
AND c.airport_ident='KMMU' AND d.name is null)x
where l.airport_ident='KMMU' and l.transition_ident=x.transition_ident
and l.star_ident=x.star_ident and l.data_supplier='J'
group by x.star_ident,x.transition_ident
order by l.star_ident,x.transition_ident,l.sequence_num";
$res3=mysqli_query($mysqli,$sql3);
if($res3)
{
while($newArray3=mysqli_fetch_array($res3,MYSQLI_ASSOC))
{
$apt=$newArray3['airport_ident'];
$star_ident=$newArray3['star_ident'];
$trans_ident=$newArray3['transition_ident'];
$fix_ident=$newArray3['fix_ident'];
$fix_seq=$newArray3['fix_seq'];
echo $apt.",".$star_ident.",".$trans_ident.",".$fix_ident.",COREPT,".$fix_seq;
echo "<br>";
}
}
else
{
printf("ERROR:%s\n",mysqli_error($mysqli));
}
Your query looks overly complex. It appears to be finding the groupwise maximum std_star_leg records by sequence_num (grouped on start_ident and transition_ident), excluding those for which there is already a matching non-self-referencing deploy_star, then returning the results grouped again with all matching fix_ident values concatenated into a string?
If so, the following greatly simplified query ought to achieve the same outcome:
SELECT GROUP_CONCAT(fix_ident SEPARATOR '-') AS fix_seq,
airport_ident,
star_ident,
transition_ident
FROM corept.std_star_leg l NATURAL JOIN (
SELECT star_ident, transition_ident,
data_supplier, airport_ident,
MAX(sequence_num) sequence_num
FROM corept.std_star_leg
WHERE data_supplier = 'J'
AND airport_ident = 'KMMU'
GROUP BY star_ident, transition_ident
) b
WHERE NOT EXISTS (
SELECT NULL
FROM skyplan_deploy.deploy_stars d
WHERE d.name != d.trans
AND d.name = l.star_ident
AND d.trans = l.fix_ident
AND d.apt = l.airport_ident
)
GROUP BY star_ident, transition_ident
Note that whereas you were previously selecting x.fix_ident in the outermost select list, I have omitted such column because its value would be indeterminately selected by the server from amongst those in the fix_seq.
Now, as to your problem (which appears to be related to the order in which fix_ident values appear within the GROUP_CONCAT() string fix_seq—although it's terribly hard to appreciate that from your question), perhaps you want to use the ORDER BY parameter to the GROUP_CONCAT() function? For example:
SELECT GROUP_CONCAT(fix_ident SEPARATOR '-' ORDER BY ...) AS fix_seq
However, it is not clear to me what ordering you require (the only ORDER BY clause in your original query was entirely redundant).
I need to fetch the following information about indices on a specific table:
index name
columns that are indexed
unique or not?
How can I do that in PostgreSQL 8.4?
NOTE: I have to be able to call this stuff with PHP. Just saying...
EDIT: I first had this query, but it only works starting with PostgreSQL 9.0:
SELECT t.relname AS table_name,
relname AS index_name,
a.attname AS column_name,
ix.indisunique
FROM pg_class t,
pg_class i,
pg_index ix,
pg_attribute a,
pg_constraint c
WHERE t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND i.oid = c.conindid
AND a.attnum = ANY(ix.indkey)
AND c.contype != 'p'
AND t.relkind = 'r'
AND t.relname = 'tablename'
ORDER BY t.relname, i.relname
You could simply use pg_indexes which will include the full CREATE TABLE statement (and therefor the information about the columns and the uniqueness).
Alternatively, the following should work:
select t.relname as table_name,
ix.relname as index_name,
array_to_string(array_agg(col.attname), ',') as index_columns,
i.indisunique
from pg_index i
join pg_class ix on ix.oid = i.indexrelid
join pg_class t on t.oid = i.indrelid
join (select ic.indexrelid,
unnest(ic.indkey) as colnum
from pg_index ic) icols on icols.indexrelid = i.indexrelid
join pg_attribute col on col.attrelid = t.oid and col.attnum = icols.colnum
where t.relname = 'tablename'
group by t.relname, ix.relname, i.indisunique
order by t.relname,
ix.relname
It doesn't return the columns in the correct order though. But I didn't have time to dig deeper into that.