I have a table that I use to keep track of some associations between users and various other aspects of their website.
I need to be able to get the first available row and update one or two of it's columns ... the criteria is whether or not the user_id column has been used or not.
id | tag_id | user_id | product_id
If a row has a tag available where there is no user_id assigned, I want to be able to use and update that row for the latest purchased product.
1 | 100001 | 29 | 66
2 | 100002 | 0 | 0
3 | 100003 | 0 | 0
So as you can see, the second row would be the first eligible candidate.
I'm just not sure what the SQL needs to be in order to make that happen
UPDATE yourTablename SET user_id = 'your value for userid',
product_id='ur value for productid' WHERE id=(select min(id) where user_id='0');
alternative method already told are efficient but if your table has sorting with id
UPDATE yourTablename SET user_id = 'your value for userid',
product_id='ur value for productid' where user_id='0' LIMIT 1;
If I understand you correctly you want to update the first available empty (not NULL but empty) user_id row. How's this?
UPDATE users
SET user_id = 'user_value_here'
WHERE user_id=''
LIMIT 1
If your index is sorted ASC, the query below should find the first result in order.
See the fiddle.
UPDATE table SET user_id = 1 WHERE user_id IS NULL LIMIT 1
You can replace IS NULL with the condition for an empty user_id.
Related
I would like to create a queue system that works in this way:
A user fills in a form where they will have to enter some data.
Click on Send and these data will be saved in a sql table.
Going to the index.php page will see a box containing a text like this: There are 4 requests in front of you, please wait a few minutes.
I have already tried to do such a thing, but going to create new requests the number "4" of the message grows.
This is because I created a query that counts all the results on the table.
$query = $mysql->query("SELECT COUNT(*) AS q FROM application_approve");
While I want it to count only the results above the request that sent the user.
id name text text2
1 First request dassasad dsadasas
2 Second request dassasad dsadasas
3 Third request dsadasdsas dsadasad
In the example above I would like to count only how many lines there are above the "Second Request": in this case 1.
Assuming your table has a PK (id) and references a user_id to identify which request belongs to which user and assuming there can only be a single request in the queue per user then your query would look something like the following.
SELECT COUNT(id) AS q FROM application_approve
WHERE id < (
SELECT id FROM application_approve
WHERE user_id = ?
)
This also assumes the PK id is an auto-incrementing key.
Given the user_id this query would return the number of rows above the given user's row (assuming they have one). Or, in other words, all ids less than the id of the given user.
For simplicity, let's assume this schema only has 2 columns (id and user_id):
mysql> SELECT * FROM application_approve;
+------+---------+
| id | user_id |
+------+---------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+---------+
3 rows in set (0.00 sec)
So in the given table, there are 3 users, each with 1 entry in the queue.
If we wanted to find which position user 2 is in the query would give us the following result:
mysql> SELECT COUNT(id) AS q FROM application_approve WHERE id < (SELECT id FROM application_approve WHERE user_id = 2);
+---+
| q |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
My table looks like this:
+------------------------+
| id | title | position |
+------------------------+
| 1 | test 2 | 3 |
+------------------------+
| 2 | test 3 | 1 |
+------------------------+
| 3 | test 1 | 0 |
+------------------------+
I found this query which retrieves the rows ordered based on the position field which holds the id of the predecessor.
SELECT
*
FROM
mytable AS t1
LEFT JOIN
mytable AS t2
ON t2.position = t1.id
I wonder why this is working because there is no order by clause and the database should't know that position 0 is the row to start at.
The result is dependent on the order you inserted the rows into the table. If, for example, you had inserted the row with id=3 before you inserted the row with id=2, then you would have got a non-sorted result.
As it stands, you are pulling the data out of t1 in the order of id because that is the order you put the elements into the table
See http://sqlfiddle.com/#!2/63a925/2 and try it for yourself.
N.B. Databases are not guaranteed to work as you state, it is simply that most databases work this way. You should not rely on this behaviour as a minor change to the schema or query could ruin your whole day! Note also that if id is a (primary?) key, the insert order will probably be overridden by the fact that the database will pull the rows out in the order of the index.
That query is joining in table 2 based on the ID in table 1 equaling the position in table 2. Since the IDs in table 1 are sequential, the output appears to be sorted
I've got two tables which are related to each other. I want to change table 1 status to updated when I insert new values into table 2 and return mysqli_affected_rows() for table 1
table 1: products
id | name
1 | product
table 1: categories
id | name | product_id
1 | cat 1 | 1
2 | cat 2 | 1
is this possible?!
You show the structure of the tables, but don't include any column that should be updated. This makes me think that you don't really want an update. You just want a query that returns whether a given product is in the categories table.
If so, the following may do what you want:
select p.*,
coalesce( (select 1 from categories c where c.productid = p.id limit 1), 0) as InCategories
from products p;
If you try to go another route where you actually store the value in the table, you will need to use a stored procedure or triggers. Do consider, though, what happens when you delete or update a row. insert is not the only way to modify data in a table.
I have a table witch has 45 columns but only a few of these are yet completed. This table is continuously updated and added etc. In my auto-complete function i want to select these records ordered by the most completed fields(i hope you understand)?
One of the solutions is to create another filed (the "rank" field) and create a php function that selects * the records and gives a rank for each record.
... but i was wondering if there is a more simple way of doing this only whit a single ORDER BY?
MySQL has no function to count the number of non-NULL fields on a row, as far as I know.
So the only way I can think of is to use an explicit condition:
SELECT * FROM mytable
ORDER BY (IF( column1 IS NULL, 0, 1)
+IF( column2 IS NULL, 0, 1)
...
+IF( column45 IS NULL, 0, 1)) DESC;
...it is ugly as sin, but should do the trick.
You could also devise a TRIGGER to increment an extra column "fields_filled". The trigger costs you on UPDATE, the 45 IFs hurt you on SELECT; you'll have to model what is more convenient.
Note that indexing all fields to speed up SELECT will cost you when updating (and 45 different indexes probably cost as much as a table scan on select, not to say that the indexed field is a VARCHAR). Run some tests, but I believe that the 45-IF solution is likely to be the best overall.
UPDATE:
If you can rework your table structure to normalize it somewhat, you could put the fields in a my_values table. Then you would have a "header table" (maybe with only a unique ID) and a "data table". Empty fields would not exist at all, and then you could sort by how many filled fields are there by using a RIGHT JOIN, counting the filled fields with COUNT(). This would also greatly speed up UPDATE operations, and would allow you to efficiently employ indexes.
EXAMPLE (from table setup to two normalized tables setup):
Let us say we have a set of Customer records. We will have a short subset of "mandatory" data such as ID, username, password, email, etc.; then we will have a maybe much larger subset of "optional" data such as nickname, avatar, date of birth, and so on. As a first step let us assume that all these data are varchar (this, at first sight, looks like a limitation when compared to the single table solution where each column may have its own datatype).
So we have a table like,
ID username ....
1 jdoe etc.
2 jqaverage etc.
3 jkilroy etc.
Then we have the optional-data table. Here John Doe has filled all fields, Joe Q. Average only two, and Kilroy none (even if he was here).
userid var val
1 name John
1 born Stratford-upon-Avon
1 when 11-07-1974
2 name Joe Quentin
2 when 09-04-1962
In order to reproduce the "single table" output in MySQL we have to create a quite complex VIEW with lots of LEFT JOINs. This view will nonetheless be very fast if we have an index based on (userid, var) (even better if we use a numeric constant or a SET instead of a varchar for the datatype of var:
CREATE OR REPLACE VIEW usertable AS SELECT users.*,
names.val AS name // (1)
FROM users
LEFT JOIN userdata AS names ON ( users.id = names.id AND names.var = 'name') // (2)
;
Each field in our logical model, e.g., "name", will be contained in a tuple ( id, 'name', value ) in the optional data table.
And it will yield a line of the form <FIELDNAME>s.val AS <FIELDNAME> in the section (1) of the above query, referring to a line of the form LEFT JOIN userdata AS <FIELDNAME>s ON ( users.id = <FIELDNAME>s.id AND <FIELDNAME>s.var = '<FIELDNAME>') in section (2). So we can construct the query dynamically by concatenating the first textline of the above query with a dynamic Section 1, the text 'FROM users ' and a dynamically-built Section 2.
Once we do this, SELECTs on the view are exactly identical to before -- but now they fetch data from two normalized tables via JOINs.
EXPLAIN SELECT * FROM usertable;
will tell us that adding columns to this setup does not slow down appreciably operations, i.e., this solution scales reasonably well.
INSERTs will have to be modified (we only insert mandatory data, and only in the first table) and UPDATEs as well: we either UPDATE the mandatory data table, or a single row of the optional data table. But if the target row isn't there, then it must be INSERTed.
So we have to replace
UPDATE usertable SET name = 'John Doe', born = 'New York' WHERE id = 1;
with an 'upsert', in this case
INSERT INTO userdata VALUES
( 1, 'name', 'John Doe' ),
( 1, 'born', 'New York' )
ON DUPLICATE KEY UPDATE val = VALUES(val);
(We need a UNIQUE INDEX on userdata(id, var) for ON DUPLICATE KEY to work).
Depending on row size and disk issues, this change might yield an appreciable performance gain.
Note that if this modification is not performed, the existing queries will not yield errors - they will silently fail.
Here for example we modify the names of two users; one does have a name on record, the other has NULL. The first is modified, the second is not.
mysql> SELECT * FROM usertable;
+------+-----------+-------------+------+------+
| id | username | name | born | age |
+------+-----------+-------------+------+------+
| 1 | jdoe | John Doe | NULL | NULL |
| 2 | jqaverage | NULL | NULL | NULL |
| 3 | jtkilroy | NULL | NULL | NULL |
+------+-----------+-------------+------+------+
3 rows in set (0.00 sec)
mysql> UPDATE usertable SET name = 'John Doe II' WHERE username = 'jdoe';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> UPDATE usertable SET name = 'James T. Kilroy' WHERE username = 'jtkilroy';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> select * from usertable;
+------+-----------+-------------+------+------+
| id | username | name | born | age |
+------+-----------+-------------+------+------+
| 1 | jdoe | John Doe II | NULL | NULL |
| 2 | jqaverage | NULL | NULL | NULL |
| 3 | jtkilroy | NULL | NULL | NULL |
+------+-----------+-------------+------+------+
3 rows in set (0.00 sec)
To know the rank of each row, for those users that do have a rank, we simply retrieve the count of userdata rows per id:
SELECT id, COUNT(*) AS rank FROM userdata GROUP BY id
Now to extract rows in "filled status" order, we do:
SELECT usertable.* FROM usertable
LEFT JOIN ( SELECT id, COUNT(*) AS rank FROM userdata GROUP BY id ) AS ranking
ON (usertable.id = ranking.id)
ORDER BY rank DESC, id;
The LEFT JOIN ensures that rankless individuals get retrieved too, and the additional ordering by id ensures that people with identical rank always come out in the same order.
I had a table like this
id | name
------------------
1 | SAM1
2 | SAM2
1 | SAM1
3 | SAM3
7 | SAM7
6 | SAM6
I need to show the results using this query
SELECT name,id FROM tblnameWHERE id IN (1,2,7,6,1)
and getting the following result
id | name
------------------
1 | SAM1
2 | SAM2
7 | SAM7
6 | SAM6
My problem is this skipped last id , ie 1 . I need something like this
id | name
------------------
1 | SAM1
2 | SAM2
7 | SAM7
6 | SAM6
1 | SAM1
With out using the loop query ( like follows ) any other method for doing this ?
$ids=array(1,2,7,6,1);
$i=0;
foreach($ids as $id){
$sql=mysql_query("SELECT * FROM tblname WHERE id=$id");
// Store value to array
}
Please help
The query
SELECT name,id FROM tblname WHERE id IN (1,2,7,6);
should show duplicate rows; e.g. if there are really in the table two distinct rows with the very same id, then the query will show them (since there's no DISTINCT keyword).
Instead, if you want to create duplicate lines starting from a table containing single lines, you have to join your table with a table having repeated 1 (in your case); another way could be to use union like this:
SELECT name, id FROM tblname WHERE id IN (1,2,7,6)
UNION ALL
SELECT name, id FROM tblname WHERE id = 1;
Edit
Since your id is a primary key, it will be unique, hence the "problem" you're experiencing. If you want to allow duplicate rows on insert, remove the primary key. If you need it, consider the possible solutions suggested above.
you must make ids unique, you have same id for different rows, therefore you can't get both rows at the same time.
What you are attempting is wrong.
Both the fields have same id and same value as well. You said id is your primary key on your table.
A primary key cannot be duplicated among the rows. That is the whole point of having a primary key
You mustn't have declared the id field as primary key.
Remove the bottom row
Add primary key to the field , run this query
ALTER TABLE `tablename` ADD PRIMARY KEY(id) AUTO_INCREMENT
Now from this point ahead, you will have unique id for all the records you have and you will have no problem on selecting the rows.
try union all
(SELECT name,id FROM tblname WHERE id IN (1,2,7,6)) UNION ALL (SELECT name,id FROM tblname WHERE id IN (1))
As you say id is primary key
you cannot insert duplicate entries for that field
hence the second insert with id 1 might have failed
SELECT name,id FROM tblname
it will not display the second entry with id 1
because primary key should be unique.
So what your are getting is what you have in Database.
You Try Out This Without Changes In Table
SELECT name,id FROM tblname WHERE id IN (1,2,7,6,1) GROUP BY id