replace statement query in CI - php

i have this table
id | part_number | desc | date |
1 | 3yrt3470 | aa | 2017-02-20 |
2 | 3utitj40 | bb | 2017-02-12 |
pk=id
i want to create query insert into table but if part_number exist it will updated. anyone can help me?
i tried this
function replace($part_number)
{
$this->db->replace('tbl_master_data');
$this->db->set('description',$description);
$this->db->set('rrp',$rrp);
$this->db->set('discount_code',$discount_code);
$this->db->set('product_group',$product_group);
$this->db->set('value_discount',$value_discount);
$this->db->set('stk',$stk);
$this->db->where('part_number',$part_number);
}
error messages show you must use the 'set' method...

Related

How to update all rows with column = to value except 1, then update that one to another value?

I have a MySQL OFFERS table with an OID, PID and STATUS. STATUS has 3 ENUM values. O A C. (Open, Accepted, Closed). It looks like this:
+---------+-------------+--------+
| oid | pid | status |
+---------+-------------+--------+
| 1 | 1 | o |
| 2 | 1 | o |
| 3 | 1 | o |
| 4 | 2 | o |
+---------+-------------+--------+
All offers are open.
When a user wants to accept an offer, they click a button, which sends the post ID (PID) and the offer ID (OID) to the php file which does the UPDATE statement to the database. I want the statement to update the table to look like this:
+---------+-------------+--------+
| oid | pid | status |
+---------+-------------+--------+
| 1 | 1 | a |
| 2 | 1 | c |
| 3 | 1 | c |
| 4 | 2 | o |
+---------+-------------+--------+
Psudo-PHP-MySQLI statement:
UPDATE offers
SET status='c'
WHERE pid = $_POST['pid'], except WHERE oid = $_POST['oid']
but also SET status='a' WHERE oid = $_POST['oid']
I am new to PHP and MySQLi so I dont know how to structure all of the statements. Is this kind of statement even possible? I know to use prepared statements, but I am just adding the values in here just for simplicity. Thanks!
Use CASE to specify different values to assign depending on a condition.
UPDATE offers
SET status =
CASE oid
WHEN $_POST['oid'] THEN 'a'
ELSE 'c'
END
WHERE pid = $_POST['pid']

how to get array in database column ? Laravel

I want to get array in database,it work in database query (my database i use postgrest) but not work in my project laravel.
Error message -> Undefined column: 7 ERROR: column sample1.text[1] does not exist
table sample1
___________________________________
| id | name | text[] |
|------+------+--------------------|
| 1 | aa | {xxx1,xxx2,xxx3} |
| 2 | bb | {xxx1,xxx2,xxx3} |
| 3 | cc | {xxx1,xxx2,xxx3} |
|______|______|____________________|
I need value
{
xxx1,
xxx1,
xxx1
}
my code
$search = DB::table("sample1")->select(array( 'sample1.text[1]'))->get();
return response()->json($search);
in my database query it work
SELECT "NSO_STAT_GIS"."BND_SET_STAT_TABLE"."MAPTB_CAT_SL_ID"[1] FROM "NSO_STAT_GIS"."BND_SET_STAT_TABLE"
$textsJson = DB::table("sample1")->select('text')->get();
$textArray = json_decode($textsJson);
https://laravel.com/docs/5.8/queries#selects

SQL select when it is different value

I have two tables and I want to select row from table_2 with different values from row employ in table_1 to show a table with registered complaints without an employing number, I have tried this sql statement:
SELECT * FROM krita_db, sjofor_db WHERE employing_nr != nr ORDER BY id DESC
but get then 3 of the same row if the
+----+--------------+-------------------------------------+
| id | employing_nr | complaint |
+----+--------------+-------------------------------------+
| 1 | 123 | something bad |
| 2 | 333 | you have to do something with this |
+----+--------------+-------------------------------------+
+----+-----+------+---------+----------+
| id | nr | navn | adresse | tlf |
+----+-----+------+---------+----------+
| 1 | 123 | ola | --- | 12345678 |
| 2 | 321 | kari | --- | 98765432 |
| 3 | 222 | gerd | --- | 12344321 |
+----+-----+------+---------+----------+
I just want to show one of the same complaint and not 3 times, how can I accomplish that?
With my code now I get this table:
+----+--------------+--------------------------------------+
| id | employing_nr | complaint |
+----+--------------+--------------------------------------+
| 1 | 123 | something bad |
| 1 | 123 | something bad |
| 2 | 333 | you have to do something with this |
| 2 | 333 | you have to do something with this |
| 2 | 333 | you have to do something with this |
+----+--------------+--------------------------------------+
I want to display this, the complaint with an employing_nr that is not registered:
+----+--------------+--------------------------------------+
| id | employing_nr | complaint |
+----+--------------+--------------------------------------+
| 2 | 333 | you have to do something with this |
+----+--------------+--------------------------------------+
Try to avoid implicit joins in the query.
You can try EXIST operator as below
SELECT *
FROM krita_db
WHERE NOT EXISTS (
SELECT 1 FROM sjofor_db
WHERE krita_db.employing_nr = sjofor_db.nr)
fiddle
Also, you can get same results using Hoàng Đăng's answer (LEFT JOIN + NULL check)
Try this
Select * FROM krita_db LEFT JOIN sjofor_db ON employing_nr = nr ORDER BY krita_db.id DESC
I assume table with complaint is krita_db
You can just use SELECT DISTINCT instead of SELECT. That will remove all your duplicate rows.

Triangular Exchange in SQL? (Via PHP)

I have a SQL table which looks like:
---------------------
| price |price_two |
---------------------
| 35.90 | 0 |
| 21.90 | 5 |
| 7.90 | 0 |
| 50.21 | 30.29 |
---------------------
And now I want to create a query in SQL which switches all values from price to price_two, where price_two is lower than price but NOT 0.
So, at the end I want this:
---------------------
| price |price_two |
---------------------
| 35.90 | 0 |
| 5 | 21.90 |
| 7.90 | 0 |
| 30.29 | 50.21 |
---------------------
Can I do that and if yes, how?
Greetings and Thank You!
Something like...
SQL Fiddle
UPDATE Price P
INNER JOIN Price P2
on P.Price = P2.Price
AND P2.Price_two = P.Price_two
AND P.Price_two < P.Price
AND P.Price_two != 0
SET P.Price = P2.Price_two,
P.Price_two = P2.Price;
We simply use a self join and use the 2nd table's data to update the first since we need to materialize the values and perform both updates at once or we lose the handle to original values!

Query mysql when reach row then stop? show the queue number

I am doing a script want to calculate how many row record before an user record when t1.status is 1.
My table is t1, and the data as below:
+------+---------+------------+----------+----------+
| ID | name | desc | status | time |
+------+---------+------------+----------+----------+
| 1 | ABB | | 1 | 0325 |
| 2 | CCD | | 1 | 0236 |
| 3 | EEF | | 1 | 0325 |
| 4 | GGG | | 1 | 0000 |
| 5 | HIJ | | 2 | 1234 |
| 6 | KKK | | 1 | 5151 |
+---------------------------------------------------+
I was thinking about the query is something like (query row where status = 1 AND stop when reach $userid)
I would like to output to show user (Let's say username is GGG) as:
$userid = 'GGG';
then my output will be
<table><tr><td>Queue: GGG You came in 4 place, in front of you still got 3 person in queue, please be patient</td></tr></table>
How to I do the right query to get the number 4 and 3 ?
Thank you.
You can try something like this hope it helps :-
SELECT count(*) as COUNT FROM t1 WHERE id < (SELECT id FROM t1 WHERE userid = $userid)

Categories