SQL select when it is different value - php

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.

Related

Dynamically create MySQL table columns

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
}

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)

multiple or single selection

i have two tables and column name are as :
Table 1
user | food | color | bike | car
Table 2
user | mobile | laptop
Now i want to get result by select single or multiple value.
For example, if i want select user which have bike and laptop . then i can get result it by query but for this all fields i have to use many condition . i have used if else where. and i also want to refine select with current selection . so what should i use ? Please help my previous question was same but i did not asked perfectly. so asked again. Thank You.
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.
Example
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-24 |
| 2 | Learn MySQL | Abdul S | 2007-05-24 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
-> FROM tutorials_tbl a, tcount_tbl b
-> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

Select foreign key (group) where is the biggest match

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.

Create an UPDATE MySQL query based on already working SELECT one

I would like to create and UPDATE MySQL query based on a SELECT statement that is already working.
So I have the following select statement that joins two tables - tbl_random and tbl_products by finding a random record from the second table:
$sql_select = "SELECT tbl_random.keyword, tbl_random.model_id,
tbl_products.make, tbl_products.model
FROM tbl_random LEFT OUTER JOIN
tbl_products
ON tbl_random.model_id = tbl_products.model
GROUP BY tbl_random.keyword
ORDER BY RAND()";
$rs_select = $db -> Execute($sql_select);
This is how tbl_random should look like after the update:
+---------+------------+---------+---------+--------------+
| keyword | model_id | make | model | more_data1 |
+---------+------------+---------+---------+--------------+
| apple1 | 15 | app5 | 15 | data1 |
| apple2 | 15 | app1 | 15 | data2 |
| pear | 205 | pear53 | 205 | data3 |
| cherry | 307 | cher74 | 307 | data4 |
| melon | 5023 | melo2 | 5023 | data5 |
+---------+------------+---------+---------+--------------+
What UPDATE query should I use in order to the able to update the make and model fields in tbl_random, with some respective random values from tbl_products?

Categories