How to use IS NULL in query from php - php

I am trying to run query from php. Part of the WHERE clause include the IS NULL.
In this case I am getting no records.
Example:
SELECT * FROM TABLE WHERE ITEM IS NULL"
no records returned. When tested directly in PL/SQL I got records.
SELECT * FROM TABLE WHERE ITEM ='cars'"
it's working fine records retreived.
Does PHP reject when using IS NULL in a query?

Is it actually NULL or is it just empty?
SELECT * FROM TABLE WHERE ITEM =''
This query will check if your row field is empty
SELECT * FROM TABLE WHERE ITEM IS NULL
will check if the row field is null
This answer provides a really good insight of their difference:
Check it here

Managed to do a work around and added AND 1=1 and worked.
SELECT * FROM TABLE WHERE ITEM IS NULL AND 1=1

Related

I want to send multiple value pairs via API and insert them into SQL DB [duplicate]

I have two tables and my goal is to move specific data from the first table into the second table along with a reason for why that data was moved. For instance:
raw_data_table
SELECT * FROM raw_data_table where id IS NULL;
I would want to move this into the second table, which is identical to the first table except for an extra column reason. I tried:
INSERT INTO bad_data_table
(SELECT * FROM raw_data_table WHERE id IS NULL), "The ID is NULL";
But this returns a syntax error. How can I copy the entire row over and add the reason value?
INSERT INTO bad_data_table
SELECT *, 'The ID is NULL' AS Reason
FROM raw_data_table
WHERE id IS NULL;
Try this query:
INSERT INTO bad_data_table VALUES (
(SELECT * FROM raw_data_table WHERE id IS NULL LIMIT 1), "The ID is NULL");
The subquery here needs to have 1 row!

Duplicate records in MySQL. EXISTS check for the same data not working properly?

SELECT EXISTS
(SELECT * FROM table WHERE deleted_at IS NULL and the_date = '$the_date' AND company_name = '$company_name' AND purchase_country = '$p_country' AND lot = '$lot_no') AS numofrecords")
What is wrong with this mysql query?
It is still allowing duplicates inserts (1 out of 1000 records). Around 100 users making entries, so the traffic is not that big, I assume. I do not have access to the database metrics, so I can not be sure.
The EXISTS condition is use in a WHERE clause. In your case, the first select doesn't specify the table and the condition.
One example:
SELECT *
FROM customers
WHERE EXISTS (SELECT *
FROM order_details
WHERE customers.customer_id = order_details.customer_id);
Try to put your statement like this, and if it returns the data duplicated, just use a DISTINCT. (SELECT DISCTINCT * .....)
Another approach for you :
INSERT INTO your_table VALUES (SELECT * FROM table GROUP BY your_column_want_to_dupplicate);
The answer from #Nick gave the clues to solve the issue. Separated EXIST check and INSERT was not the best way. Two users were actually able to do INSERT, if one got 0. A single statement query with INSERT ... ON DUPLICATE KEY UPDATE... was the way to go.

INSERT INTO table SELECT not giving correct last_id

I have 2 tables with similar columns in MYSQL. I am copying data from one to another with INSERT INTO table2 SELECT * FROM table1 WHERE column1=smth. I have different columns as autoincrement and KEY in tables. When I use mysqli_insert_id i get the first one rather then last one inserted. Is there any way to get the last one?
Thanks
There is no inherit ordering of data in a relational database. You have to specify which field it is that you wish to order by like:
INSERT INTO table2
SELECT *
FROM table1
WHERE column1=smth
ORDER BY <field to sort by here>
LIMIT 1;
Relying on the order a record is written to a table is a very bad idea. If you have an auto-numbered id on table1 then just use ORDER BY id DESC LIMIT 1 to sort the result set by ID in descending order and pick the last one.
Updated to address OP's question about mysqli_insert_id
According to the Mysql reference the function called here is last_insert_id() where it states:
Important If you insert multiple rows using a single INSERT statement,
LAST_INSERT_ID() returns the value generated for the first inserted
row only. The reason for this is to make it possible to reproduce
easily the same INSERT statement against some other server.
Unfortunately, you'll have to do a second query to get the true "Last inserted id". Your best bet might be to run a SELECT COUNT(*) FROM table1 WHERE column1=smth; and then use that count(*) return to add to the mysqli_insert_id value. That's not great, but if you have high volume where this one function is getting hit a lot, this is probably the safest route.
The less safe route would be SELECT max(id) FROM table2 or SELECT max(id) FROM table2 Where column1=smth. But... again, depending on your keys and the number of times this insert is getting hit, this might be risky.

update with max value of the column is not working in mysql

I have tried to set the max value for the particular column but that is not working for me. I do not know where i'm going wrong.
UPDATE `upload_video`
SET order_id ='select max(order_id)+1
FROM upload_video'
WHERE `video_id` = 22
This is my query i run the select max(order_id)+1 from upload_video query separately which is giving the result. But if i use this query in update query, the query is executing without error. But the order_id is not updating properly. please help me
Your query is almost correct in standard SQL, you only need to use brackets () instead of apostrophe ':
SET order_id = (SELECT MAX(...) ...)
but MySQL doesn't allow you to update a table while selecting from the same table, a workaround is to use a subquery that calculates the value that you need, and to join your subquery with the table you need to update:
UPDATE
upload_video JOIN (SELECT COALESCE(MAX(order_id),0)+1 max_id
FROM upload_video) s
SET
upload_video.order_id=s.max_id
WHERE
video_id=22
Please see fiddle here.
You have a typo in the statement, you used UPADTE instead of UPDATE.
One problem is, don't quote the subquery. You have used single quotes, which means the expression select max(order_id)+1... was interpreted as a text literal (a varchar). But you clearly don't want that (I guess order_id is a number). What you want instead is to evaluate the subquery. However, if you try:
UPDATE `upload_video`
SET order_id =(select max(order_id)+1
FROM upload_video)
WHERE `video_id` = 22
then MySQL doesn't allow it (I didn't know about that). Other databases such as PostgreSQL allow it. So you might need two statements:
select #id = coalesce(max(order_id), 0) + 1 FROM upload_video;
UPDATE `upload_video` SET order_id = #id WHERE `video_id` = 22;
Please note this works in MySQL but not in other databases.
Try this:
UPDATE `upload_video`
SET order_id =(select COALESCE(max(U2.order_id),0)+1
FROM upload_video U2)
WHERE `video_id` = 22
Peraphs this query goes in error because MySql doesn't want to use the same table in UPDATE and in subquery.
If your case please write two queries.
The first get the maximum value, the second does update

Display only the columns that contains data

I have a database that have 35 columns and some of those columns are empty and I've already made the page and the query to retrieve the data from the data base but the problem is All the data including the empty ones, and I want to know if there is a resolving to these problem.
you can use the where clause in the query
like
Select * from TableName where FieldName is Not Null And Field2 is not null
Similarly you can add as many filters as you want
Use the WHERE clause:
WHERE somefield IS NOT NULL
Your query (assuming you're using SQL) should have something like
... my_table.my_field IS NOT NULL ...
see also: MySQL select where column is not empty

Categories