I have a mysql update here and when the user make changes to one selection category it would duplicate that selection to the rest of the other selection too. Below is a sample scenario where only the 1st CD category was change and it's reflecting the same for the rest. How do I get "nmc_category.catDesc ='$pCDCategory" to only update that entry only ?
Sample:
$sql = "UPDATE nmc_cd "
. "JOIN nmc_category ON (nmc_cd.catID = nmc_category.catID) "
. "JOIN nmc_publisher ON (nmc_cd.pubID = nmc_publisher.pubID) "
. "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_category.catDesc ='$pCDCategory', nmc_publisher.pubName = '$pCDPubName' , nmc_cd.pubID ='$pCDPubID', nmc_publisher.pubID='$pCDPubID' "
. "WHERE nmc_cd.CDID='$pCDID'";
nmc_cd table:
nmc_category table:
use this command
SET SQL_SAFE_UPDATES=0;
Related
I have a terrible to identify unique record(value).
I have a table like this:
ID NAME DESCRIPTION
1 Yanagida Fumit best author
2 Ha Il-kwan new author
3 Fumit Yanagida best author
4 Ha Il Kwan new author
5 Ilkwan Ha new author
There are 5 records in same table called autho table. But in actually, there are 2 authors.
First record and third record are stored from one author information and second, 4th and 5th are one author.
I want to make this like below.
ID NAME DESCRIPTION
1 Yanagida Fumit best author
2 Ha Il Kwan new author
It means that, I am going to erase all duplicates against reverse name problem.
I wonder if I can compare two values(string) in same column.
Help me please. I will be happy with your any help!
Here is a php solution:
remove_duplicates("Yanagida Fumit");
function remove_duplicates($full_search_str) {
// establish connection to your db
// fetch data
$query = " SELECT `id`, `name`
FROM `" . $tbl_name . "`
WHERE MATCH (`name`) AGAINST ('" . $full_search_str . "' IN BOOLEAN MODE)
AND `name` <> '" . $full_search_str . "'
";
// run query
$results = $conn->query($query);
// loop through results
foreach($results as $result) {
// build query
$query = " DELETE
FROM `" . $tbl_name . "`
WHERE `id` = " . $result['id'];
// run query
$result = $conn->query($query);
}
}
Oracle answer:
create or replace table authors as
Select distinct name, description
from authors;
I am trying to follow the following blog and use an UPDATE, IF, INSERT INTO statement seeing that it will no go over my data twice.
Please note it is for php.
The statement that I have is as follows
$query = "UPDATE
" . $this->table_name2 . "
SET
batch = :batch,
created = :created
WHERE
id = :id
IF row_count() = 0
INSERT INTO " . $this->table_name2 . "
SET
id=:id,
batch=:batch,
created=:created";
But my return value always comes back as false, and I do not know where the problem is.
If I try the first half of the statement it updates the information:
$query = "UPDATE
" . $this->table_name2 . "
SET
batch = :batch,
created = :created
WHERE
id = :id
And if I try the second half of the statement it INSERTS the information:
INSERT INTO " . $this->table_name2 . "
SET
id=:id,
batch=:batch,
created=:created";
I do not find any help after allot of searching so far, and I feel that somehow my IF statement may not be correct, but I do not even get info on the IF row_count() = 0 in the docs.
The IF statement is available in stored procedures only.
If id is the primary key (or unique) this should work for you:
INSERT INTO " . $this->table_name2 . "
SET
id = :id,
batch = :batch,
created = :created
ON DUPLICATE KEY UPDATE
batch = :batch,
created = :created
This will execute the INSERT statement only, of the ID does not exits yet. If it already exists, it will execute the UPDATE part.
I have a name query like the one below - but keep getting an error from Hibernate that it will not allow my 'with'-clause since I'm doing join fetch.
I need to do join fetch - because it there is a related item then I wan't it to be present on the object.
#NamedQuery(name = "Item.findItem",
query = "SELECT DISTINCT(c) FROM Item c " +
"JOIN FETCH c.storeRelations as childStoreRelation " +
"LEFT JOIN FETCH c.relatedItems as relatedRelations WITH relatedRelations.store.id = childStoreRelation.store.id " +
"WHERE c.id = :itemId " +
"AND childStoreRelation.store.id = :storeId " +
"AND childStoreRelation.deleted <> 'Y' " +
"ORDER BY c.partnumber "),
Suggestion would be to move the 'with' part to my where clause - but this will cause invalid results.
Consider item A which might have to items related to it - but for some stores the relations are not valid.
If put in where clause then the store without relations will not shown the main item, since SQL will be build by Hibernate so it requires that if any relations exist, then they must be valid for the store - otherwise nothing is fetched :-(
So the classic fix found many places (store.id is null OR store.id = :storeId) will not work.
Does someone have another work around?
I'm running Hibernate 4.3.11
Thanks in advance
You cannot use LEFT JOIN FETCH with clause.
Please remove WITH clause and move them to WHERE clause.
It should like this:
#NamedQuery(name = "Item.findItem",
query = "SELECT DISTINCT(c) FROM Item c " +
"JOIN FETCH c.storeRelations as childStoreRelation " +
"LEFT JOIN FETCH c.relatedItems as relatedRelations " +
"WHERE c.id = :itemId " +
"AND relatedRelations.store.id = childStoreRelation.store.id" +
"AND childStoreRelation.store.id = :storeId " +
"AND childStoreRelation.deleted <> 'Y' " +
"ORDER BY c.partnumber "),
You need the ON keyword rather than the WITH keyword.
As I know I can't use a where clause inside a Insert statement but in here what I want to do is I want to insert a new row if same kind of row not exist else I need to update that row. so I need to use where clause to do that. this is my query but it is not working because of where clause. can someone help me on this.
Thank You.
"INSERT INTO "
. "`leaves_count` "
. "(`leave_cat_id`, `leave_days_count`, `emp_number`) "
. "VALUES ('$req_lv_cat', $req_lv_day_count, '$empNmbr') "
. "ON DUPLICATE KEY UPDATE "
. "leave_days_count = leave_days_count + ($req_lv_day_count) "
. "WHERE "
. "(leave_cat_id = '$req_lv_cat' AND emp_number = '$empNmbr')"
Please use merge statement. It will do "If exist UPDATE else INSERT".
MERGE <hint> INTO <table_name>
USING <table_view_or_query>
ON (<condition>)
WHEN MATCHED THEN <update_clause>
WHEN NOT MATCHED THEN <insert_clause>;
Here i am posting example i hope it will help to you
INSERT INTO table (id,a,b,c,d,e,f,g)
VALUES (1,2,3,4,5,6,7,8)
ON DUPLICATE KEY
UPDATE a=a, b=b, c=c, d=d, e=e, f=f, g=g;
I have created two tables as order & product.
In order table, order details are stored & in product table according to order, product details are stored. Now I want to give row no./line no. for that product table.
Say for order no. 100 there are 3 prodcuts-:
1 Nokia 8000 20
2 Samsung 5000 50
3 Sony 7000 30
So how to give row no/line no. for product in Mysql? Is there any function for the same?
And also that no. must start from 1 for each order.
Say after order no. 100, for order no. 101, it would be again start from 1,2...like this.
I have maintained proper PK & FK...like PK of order is FK for product table..
My Schema is like this-:
First I am inserting data in Order table using insert query so that will generate id i.e. mysql_insert_id() & I am using that for reference in Product table.
Now my code is like this-:
global $id;
$id = mysql_insert_id();
$query2 = "SELECT a.name, a.sku, a.qty_ordered, a.price, a.row_total, a.base_subtotal,
b.base_shipping_amount, b.base_grand_total,".$id."
FROM sales_flat_order b
JOIN sales_flat_order_item a
ON a.order_id = b.entity_id
WHERE b.increment_id = ". $order_id ;
$result2 = mysql_query($query2);
while($row = mysql_fetch_array($result2))
{
$result_str_product .= "('". $row["name"] . "',". "'" . $row["sku"] . "'," . "'" . $row["qty_ordered"] . "',". "'" . $row["price"] . "'," . "'" . $row["row_total"] . "'," . "'" . $row["base_subtotal"]. "'," . "'" . $row["base_shipping_amount"] . "'," . "'" . $row["base_grand_total"] ."',". $id."),";
}
$query3 = "INSERT into product(name, sku, qty_ordered, price, row_total, base_subtotal, base_shipping_amount,base_grand_total,prod_f) VALUES ".$result_str_product;
$final_result = substr_replace($query3,";",-1);
$result_query_product = mysql_query($final_result);
Now my requirement is that I want to generate line no./row no. for these products like I explain above(Plz chek my above example that Nokia,Samsung.
So How I can give row no./line no. for these item? Any in-built function for the same that I can use in while loop or any other approach?