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)
Related
I have the following MySQL table which is structured like that:
| id | bonus0 |
Now I want to add the following data set:
| id | bonus0 | bonus1 | bonus2 | bonus3 |
| 10 | 4582 | 2552 | 8945 | 7564 |
As you can see the columns bonus1 - bonus3 aren´t created yet.
How would a php script/ query look like which checks if enough columns are already available and if not which will create the missing ones with consecutive numbers at the end of the word "bonus"?
So in the example the columns bonus1 - bonus3 would be created automatically by the script.
In reality (I mean a normalized relational database) you should have 3 tables. Lets call them people, bonuses and bonus_to_person
people looks like:
+-----------------+------------+
| person_id | name |
+_________________+____________+
| 1 | john |
+-----------------+------------+
| 2 | frank |
+-----------------+------------+
bonuses Looks like
+----------------+--------------+
| bonus_id | amount |
+________________+______________+
| 1 | 1000 |
+----------------+--------------+
| 2 | 1150 |
+----------------+--------------+
| 3 | 1200 |
+----------------+--------------+
| 4 | 900 |
+----------------+--------------+
| 5 | 150 |
+----------------+--------------+
| 6 | 200 |
+----------------+--------------+
bonus_to_person Looks like
+----------------+-----------------+
| bonus_id | person_id |
+________________+_________________+
| 1 | 1 |
+----------------+-----------------+
| 2 | 2 |
+----------------+-----------------+
| 3 | 2 |
+----------------+-----------------+
| 4 | 1 |
+----------------+-----------------+
| 5 | 1 |
+----------------+-----------------+
| 6 | 1 |
+----------------+-----------------+
This way, any ONE person can have unlimited bonuses simply by INSERTING into bonuses with the amount, and INSERTING into bonus_to_person with the bonus_id and person_id
The retrieval of this data would look like
SELECT a.name, c.amount from people a
LEFT JOIN bonus_to_people b
ON a.person_id = b.person_id
LEFT JOIN bonuses c
ON c.bonus_id = b.bonus_id
WHERE a.person.id = 1;
Your result from something like this would look like
+------------+----+-------+
| name | amount |
+____________+____________+
| john | 1000 |
+------------+------------+
| john | 900 |
+------------+------------+
| john | 150 |
+------------+------------+
| john | 200 |
+------------+------------+
You should be using this normalized approach for any database that will continue growing -- Growing "deeper" than "wider" is better in your case ..
// Get existing columns of the table
// $queryResult = run SQL query using PDO/mysqli/your favorite thing: SHOW COLUMNS FROM `table`
// Specify wanted columns
$search = ['bonus0', 'bonus1', 'bonus2', 'bonus3'];
// Get just the field names from the resultset
$fields = array_column($queryResult, 'Field');
// Find what's missing
$missing = array_diff($search, $fields);
// Add missing columns to the table
foreach ($missing as $field) {
// Run SQL query: ALTER TABLE `table` ADD COLUMN $field INT
}
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.
I have three tables group_sentences, group_sentences_attributes and group_senteces_categories.
I have an attributes array which I am using in query with IN (after implode).
Then I have one category ID because they are stored recursively, so no need for an array.
I need to select one group number where is the biggest match for $attributesArray and of course category too.
Here is table group_sentences_attributes
+-----+-------+-----------+
| id | group | attribute |
+-----+-------+-----------+
| 1 | 1 | 3564 |
| 2 | 1 | 3687 |
| 3 | 1 | 3689 |
| 4 | 2 | 3687 |
| 5 | 2 | 3564 |
+-----+-------+-----------+
Here is group_sentences_category
+-----+-------+----------+
| id | group | category |
+-----+-------+----------+
| 1 | 1 | 1564 |
| 2 | 1 | 1221 |
| 3 | 1 | 1756 |
| 4 | 2 | 1358 |
| 5 | 2 | 1125 |
+-----+-------+----------+
Here is my query, but I am afraid that it won't do the job done.
SELECT group_categories.group
FROM group_categories, group_attributes
WHERE group_categories.category = '$category'
AND group_attributes.attribute IN ($attributesArray)
GROUP BY group_categories.group
ORDER BY count(group_attributes.attribute)
Any help would be appreciated, thanks.
First, the table in your query do not match the tables in the question. I am guessing they are simply missing the "sentence". Then, you have no join clause. Simple rule: Never use commas in the from clause.
group is a lousy name for a column, because it is a keyword in SQL. The following may be what you are looking for:
SELECT gc.groupid
FROM group_sentences_attributes sa JOIN
group_sentences_category sc
ON sa.groupid = sc.groupid
WHERE sc.category = '$category' AND
sa.attribute IN ($attributesArray)
GROUP BY sa.groupid
ORDER BY count(sa.attribute);
If you only want one row, then add LIMIT 1 to the end.
I have two table 'users' and 'friends' I am having difficulty joining them
users table
id | name | usercode
--------------------
1 | david | 2WM
2 | Samme | E5N
3 | Awudu | C0Q
4 | John | VX6
5 | Jerem | FG3
Friends Table
id | actor | target
--------------------
1 | E5N | FG3
2 | 2WM | VX6
3 | FG3 | 2WM
4 | C0Q | VX6
5 | FG3 | VX6
Basically i want to select all users from USERS table who has 'FG3' in either target or actor column in the FRIENDS table.
The result will be
id | name | usercode | actor | target
--------------------------------------
2 | Samme | E5N | E5N | FG3
1 | david | 2WM | FG3 | 2WM
5 | John | VX6 | FG3 | VX6
I have triend everything i know but still i am not getting the correct results
I will be glad if anyone can help me since I need to present this work tomorrow morning. Thank you
Looks like you want to join on usercode equals actor or target, then put the 'FG3' part in a WHERE clause:
SELECT users.id, users.name, users.usercode, friends.actor, friends.target
FROM users
INNER JOIN friends
ON users.usercode = friends.actor OR users.usercode = friends.target
WHERE users.usercode != 'FG3'
AND (friends.actor = 'FG3' OR friends.target = 'FG3');
Using INNER JOIN limits your query to only records that exist in both tables.
hello can somebody give me idea how to do this problem? let say for example i have two table "sales" and "rsales" . and i have this ff data for my tables.
data from sales
receipt | date | total | 1st_id | last_id |
129999 | 09/26/2013 | 1220 | 1 | 2 |
139999 | 09/27/2013 | 2320 | 3 | 4 |
data from rsales
id | product_name |
1 | 33uf |
2 | 44uf |
3 | 7sss |
4 | 8sss |
and this is my view from my web page when do reports
receipt | date | total | 1st_id | last_id | + |
129999 | 09/26/2013 | 1220 | 1 | 2 | + |
139999 | 09/27/2013 | 2320 | 3 | 4 | + |
so here it is. my problem is that when i click "+" it will select data from rsales and then display. so let say i cllick "+" where receipt is 129999 . so the expected output must be something like this.
receipt | date | total | 1st_id | last_id | + |
129999 | 09/26/2013 | 1220 | 1 | 2 | + |
id | product_name |
1 | 33uf |
2 | 44uf |
139999 | 09/27/2013 | 2320 | 3 | 4 | + |
so why it display like this? it is because 1st_id and last_id from sales = id 1 and 2 from rsales. it is hard to explain but i think the example i gave is enough to understand what i want to do. hoping your idea. i really need idea.
if you have it set up the way I think you have it set up you'll need two queries:
SELECT 1st_id, last_id FROM sales WHERE receipt=?
and then
SELECT Id, product_name FROM rsales WHERE Id>=[variable from 1st_id] AND Id<=[variable from last_id];
if I were doing it I would have set up 3 tables
a sales_rsales with 3 fields, id, sales_receipt, rsales_id.Get rid of 1st_id and last_id use that to track the products per receipt then the SQL will be easy:
SELECT rsales.Id, rsales.product
FROM rsales JOIN sales_rsales ON rsales.Id=sales_rsales.rsales_id
JOIN sales ON sales.receipt=sales_rsales.sales_receipt
WHERE sales.receipt=?