Stop duplicate rows in dynamic drop down - php

I have two tables, users and lifts.
On my UI (php webpage), I have two drop downs (as well as others, irrelevant), one drop down is [userlastname, userfirstname], and the other is [destination]
the query i am using is this:
SELECT * FROM lifts l JOIN users u ON l.user_id=u.uid GROUP BY l.id;
which gives me the list of usernames in users (correctly)
[Harvey Fletcher]
[Ronald McDonald]
[Harvey Fletcher]
and also a list of all destinations
[Destination 1]
[Destination 2]
[Destination 3]
But as you can see from the usernames list, [Harvey Fletcher] has appeared twice. How can I stop this from happening? Table structure below.
Users:
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| uid | int(10) | NO | PRI | NULL | auto_increment |
| title | varchar(10) | NO | | NULL | |
| firstname | varchar(100) | NO | | NULL | |
| lastname | varchar(100) | NO | | NULL | |
| email | varchar(500) | NO | | NULL | |
| password | varchar(500) | NO | | NULL | |
| reg_no | varchar(10) | YES | | NULL | |
| vehicle_make | varchar(50) | YES | | NULL | |
| vehicle_colour | varchar(25) | YES | | NULL | |
| licence_no | varchar(50) | YES | | NULL | |
| address_line1 | varchar(75) | NO | | NULL | |
| address_line2 | varchar(75) | NO | | NULL | |
| town_city | varchar(75) | NO | | NULL | |
| postcode | varchar(10) | NO | | NULL | |
| reputation_positive | int(10) | YES | | 0 | |
| reputation_negative | int(10) | YES | | 0 | |
| telephone_number | char(11) | NO | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
Lifts:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| user_id | int(10) | NO | | NULL | |
| lift_to | varchar(50) | NO | | NULL | |
| lift_from | varchar(50) | NO | | NULL | |
| quote_price | varchar(50) | NO | | NULL | |
| available_seats | int(2) | YES | | 1 | |
| depart_date | varchar(20) | NO | | NULL | |
| depart_time | varchar(10) | NO | | NULL | |
| pickup_location | varchar(100) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
Info:
Harvey Fletcher is only in users table once
Harvey Fletcher has two 'lifts', which is why it appears twice.
Ronald McDonald only has one, so only appears once, but if I added a second lift, would be displayed 2 times.

Group your query to users instead of lifts:
SELECT * FROM lifts l JOIN users u ON l.user_id=u.uid GROUP BY u.uid;

Related

get users that did not buy a product

I have three tables
users table
+--------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| AccountNumber | varchar(50) | YES | | NULL | |
| DisplayName | varchar(255) | YES | MUL | NULL | |
| ParentAccountId | bigint | YES | MUL | NULL | |
| AccountTypeRef | bigint | YES | MUL | NULL | |
| PhoneNo | varchar(255) | YES | | NULL | |
| FaxNo | varchar(50) | YES | | NULL | |
| Website | varchar(200) | YES | | NULL | |
| Email | varchar(100) | YES | | NULL | |
| StateRef | bigint | YES | MUL | NULL | |
| Address | longtext | YES | | NULL | |
| PostalCode | varchar(50) | YES | | NULL |
products table
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| ProductId | bigint | YES | MUL | NULL | |
| PackName | varchar(255) | YES | | NULL | | |
| Price | double | YES | | NULL | |
| OnlineInfoAddress | varchar(255) | YES | | NULL | |
| Description | longtext | YES | | NULL | |
| IsDelete | tinyint | YES | | NULL | |
| CreateUserId | bigint | YES | | NULL | |
| CreateDate | datetime | YES | | NULL | |
| EditDate | datetime | YES | | NULL | |
| EditUserId | bigint | YES | | NULL | |
| Tag | int | YES | | NULL | |
| Ref | varchar(50) | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
users_buy table
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| users_id | bigint | YES | MUL | NULL | |
| product_id | bigint | YES | MUL | NULL | |
| BuyDate | datetime | YES | | NULL | |
| CountBuy | int | YES | | NULL | |
| TotalPrice | double | YES | | NULL | |
| Description | longtext | YES | | NULL | |
| invoiceNumber | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
I want to get users who did not buy a specific product
for example, user number 20 bought 5 product with ids [1,2,3,4,5]
and user number 19 bought 3 product with ids [1,2,3]
And user number 18 bought 2 product with ids [1,3]
I want to get users who did not buy any product with the id 5
ie I want user 18 and 19
How can I do this?
This will list of all users that did not buy product id 5:
SELECT *
FROM users
WHERE
Id NOT IN (
SELECT users_id
FROM users_buy
WHERE product_id = 5
)
One more solution used LEFT JOIN:
SELECT users.*
FROM users
LEFT JOIN users_buy
ON users_buy.users_id = users.id AND users_buy.product_id = 5
WHERE users_buy.user_id IS NULL;
The above query will return records from users table have not matched rows in users_buy table with product_id 5

MySQL terminal results != PHP->MySQL results

I'm trying to figure out why results returned via a MySQL terminal query don't match those returned by a MySQL query executed by PHP.
Here's the MySQL used in the shell:
SELECT * FROM SANKEY_NODE AS n
LEFT OUTER JOIN TYPE_DETAIL as td
ON n.TYPE_DETAIL_ID = td.TYPE_DETAIL_ID
LEFT OUTER JOIN GRAPH_TYPE as t
ON td.GRAPH_TYPE_ID = t.GRAPH_TYPE_ID
WHERE CHART_ID = 39;
Here's the PHP:
function get_nodes_by_chart_id($con, $chart_id) {
$sql = 'SELECT * FROM SANKEY_NODE AS n
LEFT OUTER JOIN TYPE_DETAIL as td
ON n.TYPE_DETAIL_ID = td.TYPE_DETAIL_ID
LEFT OUTER JOIN GRAPH_TYPE as t
ON td.GRAPH_TYPE_ID = t.GRAPH_TYPE_ID
WHERE CHART_ID = '.$chart_id.';';
$result = mysqli_query($con, $sql);
return results_to_array($result);
}
function results_to_array($results) {
$rows = array();
while($row = mysqli_fetch_assoc($results)) {
$rows[] = $row;
}
return $rows;
}
Both queries return a dozen results, however, they differ in their representation of three results. Those three results are special because they have a TYPE_DETAIL_ID value in the SANKEY_NODE table that is not present in the TYPE_DETAIL table.
In the MySQL shell, the TYPE_DETAIL_ID value for the three affected nodes is displayed, whereas in the results returned by PHP, the TYPE_DETAIL_ID value is null. Does anyone know what might cause this difference? I'd be very grateful for any insight others can offer!
Table Structures
mysql> describe SANKEY_NODE;
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| NODE_NAME | varchar(100) | NO | | NULL | |
| NODE_PARENT | varchar(20) | YES | | NULL | |
| CHART_ID | int(11) | NO | PRI | NULL | |
| NODE_TYPE | varchar(100) | NO | | NULL | |
| TYPE_DETAIL_ID | varchar(20) | NO | PRI | NULL | |
+----------------+--------------+------+-----+---------+-------+
mysql> describe TYPE_DETAIL;
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| COMPANY_ID | int(11) | NO | | NULL | |
| GRAPH_TYPE_ID | int(11) | NO | PRI | NULL | |
| TYPE_DETAIL_CD | varchar(20) | NO | PRI | NULL | |
| TYPE_DETAIL_NAME | varchar(100) | NO | | NULL | |
| TYPE_DETAIL_DESC | varchar(200) | YES | | NULL | |
| TYPE_DETAIL_ID | int(11) | NO | | NULL | |
| TYPE_IMAGE_ID | int(11) | YES | | NULL | |
| ACTIVE_FLAG | bit(1) | NO | | NULL | |
+------------------+--------------+------+-----+---------+-------+
mysql> describe GRAPH_TYPE;
+----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+----------------+
| COMPANY_ID | int(11) | NO | PRI | NULL | |
| GRAPH_TYPE_ID | int(11) | NO | UNI | NULL | auto_increment |
| TYPE_CD | varchar(20) | NO | PRI | NULL | |
| TYPE_NAME | varchar(100) | NO | | NULL | |
| TYPE_COLOR | varchar(50) | NO | | NULL | |
| TYPE_HIGHLIGHT_COLOR | varchar(50) | NO | | NULL | |
| ACTIVE_FLAG | bit(1) | NO | | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
Comment Responses
#Cedric, running the query with just the first JOIN yields the same results. The three values whose TYPE_DETAIL_ID is present in SANKEY_NODE but not in TYPE_DETAIL_ID have a defined TYPE_DETAIL_ID in the shell results but not in the results via PHP (see results below). As for syntax, I'm a fan of the philosophy that "explicit is better than implicit".
#jcaron, the TYPE_DETAIL_ID values are either sequences of integers or ascii strings, e.g.:
mysql> SELECT * FROM SANKEY_NODE AS n LEFT OUTER JOIN TYPE_DETAIL as td ON n.TYPE_DETAIL_ID = td.TYPE_DETAIL_ID WHERE CHART_ID = 3;
+--------------------------+-------------+----------+-------------+----------------+------------+---------------+----------------+--------------------+--------------------+----------------+---------------+-------------+
| NODE_NAME | NODE_PARENT | CHART_ID | NODE_TYPE | TYPE_DETAIL_ID | COMPANY_ID | GRAPH_TYPE_ID | TYPE_DETAIL_CD | TYPE_DETAIL_NAME | TYPE_DETAIL_DESC | TYPE_DETAIL_ID | TYPE_IMAGE_ID | ACTIVE_FLAG |
+--------------------------+-------------+----------+-------------+----------------+------------+---------------+----------------+--------------------+--------------------+----------------+---------------+-------------+
| CRD | SYS | 3 | System | 101004 | 7777 | 1 | CRD | Charles River | Charles River | 101004 | NULL | |
| FactSet | SYS | 3 | System | 101012 | 7777 | 1 | FACTSET | Factset | Factset | 101012 | NULL | |
| MSCI | SYS | 3 | System | 101016 | 7777 | 1 | RISKMETRICS | MSCI RiskWorld | MSCI RiskWorld | 101016 | NULL | |
| Trade Execution | FUN | 3 | Function | 109007 | 7777 | 9 | TE | Trade Execution | Trade Execution | 109007 | NULL | |
| Portfolio Mgmt | FUN | 3 | Function | 109003 | 7777 | 9 | PM | Portfolio Mgmt | Portfolio Mgmt | 109003 | NULL | |
| Performance & Risk | FUN | 3 | Function | 109002 | 7777 | 9 | PMR | Performance & Risk | Performance & Risk | 109002 | NULL | |
| Operations | FUN | 3 | Function | 109006 | 7777 | 9 | OPS | Operations | Operations | 109006 | NULL | |
| Decision Making | FUN | 3 | Function | 109001 | 7777 | 9 | DM | Decision Making | Decision Making | 109001 | NULL | |
| Compliance | FUN | 3 | Function | 109005 | 7777 | 9 | COMP | Compliance | Compliance | 109005 | NULL | |
| Portfolio Rebalance | SFUN | 3 | SubFunction | 201091 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Position Reconciliation | SFUN | 3 | SubFunction | 201092 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| PreTrade Compliance | SFUN | 3 | SubFunction | 201096 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Step-outs | SFUN | 3 | SubFunction | 201109 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Trade Matching | SFUN | 3 | SubFunction | 201125 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Trade Settlement | SFUN | 3 | SubFunction | 201129 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Functions | | 3 | Function | FUN | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| SubFunction | | 3 | SubFunction | SFUN | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Systems | | 3 | System | SYS | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------------------------+-------------+----------+-------------+----------------+------------+---------------+----------------+--------------------+--------------------+----------------+---------------+-------------+
18 rows in set, 225 warnings (0.00 sec)
Here is an easy workaround, specify the table name for each column :
SELECT SANKEY_NODE.TYPE_DETAIL_ID FROM SANKEY_NODE AS n
That way, you will be sure to have the data that you expect

why i am getting an error of incorrect integer value on age? while i am using the correct data type and values in it?

insert into `patient` ( `pat_f_name`, `pat_l_name`, `pat_gender`, `pat_age`, `pat_dob`, `pat_blood_grp`, `pat_weight`, `pat_contact_no`, `pat_address` )
-> VALUES
-> ('$user_fname','$user_lname','$user_gender','$user_age','$user_dob','$user_bg','$user_weight','$user_contact_no','$user_address');
ERROR 1366 (HY000): Incorrect integer value: '$user_age' for column 'pat_age' at row 1
the table looks like
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| pat_id | int(50) | NO | PRI | NULL | auto_increment |
| dr_id | int(50) | NO | | NULL | |
| pat_f_name | char(20) | NO | | NULL | |
| pat_l_name | char(20) | NO | | NULL | |
| pat_gender | char(20) | NO | | NULL | |
| pat_age | int(20) | NO | | NULL | |
| pat_dob | date | NO | | NULL | |
| pat_blood_grp | varchar(10) | NO | | NULL | |
| pat_weight | int(10) | NO | | NULL | |
| pat_contact_no | varchar(12) | NO | | NULL | |
| pat_address | varchar(20) | NO | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
$sql = "INSERT INTO patient(pat_f_name, pat_l_name, pat_gender, pat_age, pat_dob, pat_blood_grp, pat_weight, pat_contact_no, pat_address) VALUES ('$user_fname','$user_lname','$user_gender','$user_age','$user_dob','$user_bg','$user_weight','$user_contact_no','$user_address')";

Error (1054) : Unknown column 'enable_flag' in 'field_list'

I was bee-bopping along writing code and testing it when I hit this error.
update products set enable_flag='Y' where prod_id=31745
ERROR # 214 : (1054)
Unknown column 'enable_flag' in 'field list'
The "enable_flag" field is listed in the table. I have tried several variants of the above SQL command. I've put backticks around "products", around "enable_flag", I've tried retyping the command - all to no avail. I even thought that maybe I'd exceeded the length limit of 65,536 characters - but no. So I came here, read up on other's having a similar problem but those were all "You mistyped X" or "You left off the name of the table" and so forth. This has got me a bit stumped. I'm using MySQL (latest version), PHP 5.4.12, and before yesterday I have mainly worked with the first half of the table. So now I'm starting to work with the second half and this happens. Also, enable_flag is column 52. I know you can have 4096 columns. However, anything from column 52 on can not be updated, inserted, etc....
Here is the table "products" dump:
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| prod_id | bigint(20) | NO | PRI | NULL | auto_increment |
| sku | varchar(40) | YES | | NULL | |
| upc | varchar(40) | YES | | NULL | |
| title | text | YES | | NULL | |
| long_desc | mediumtext | YES | | NULL | |
| cat_id | int(11) | YES | | NULL | |
| size | varchar(80) | NO | | NULL | |
| color | varchar(80) | NO | | NULL | |
| model | varchar(80) | YES | | NULL | |
| quantity | int(11) | YES | | NULL | |
| stock_status_id | int(11) | YES | | NULL | |
| image_id | bigint(20) | YES | | NULL | |
| mfg_id | int(11) | YES | | NULL | |
| shipping | decimal(10,2) | YES | | NULL | |
| price | decimal(10,2) | YES | | NULL | |
| points | decimal(10,2) | YES | | NULL | |
| tax_class_id | int(11) | YES | | NULL | |
| weight | decimal(10,2) | YES | | NULL | |
| width | decimal(10,2) | YES | | NULL | |
| length | decimal(10,2) | YES | | NULL | |
| height | decimal(10,2) | YES | | NULL | |
| viewed | int(11) | YES | | NULL | |
| date_used | datetime | YES | | NULL | |
| date_added | datetime | YES | | NULL | |
| date_modified | datetime | YES | | NULL | |
| date_available | datetime | YES | | NULL | |
| sellor_id | int(11) | YES | | NULL | |
| sellor_product_id | varchar(80) | YES | | NULL | |
| state_codes | text | YES | | NULL | |
| ground_ship_only | varchar(1) | YES | | n | |
| adult_sig_req | varchar(1) | YES | | n | |
| no_drop_ship | varchar(1) | YES | | n | |
| priority | int(3) | YES | | NULL | |
| unlimited_stock | varchar(1) | YES | | NULL | |
| reorder_level | int(11) | YES | | NULL | |
| license_key | text | YES | | NULL | |
| store_cost | decimal(10,2) | YES | | NULL | |
| msrp | decimal(10,2) | YES | | NULL | |
| handling_cost | decimal(10,2) | YES | | NULL | |
| min_order_quantity | int(11) | YES | | NULL | |
| max_order_quantity | int(11) | YES | | NULL | |
| weight_major | decimal(10,2) | YES | | NULL | |
| weight_minor | decimal(10,2) | YES | | NULL | |
| warehouse | text | YES | | NULL | |
| shipping_msg | text | YES | | NULL | |
| category_special | varchar(1) | YES | | NULL | |
| homepage_special | varchar(1) | YES | | NULL | |
| shipping_option | text | YES | | NULL | |
| product_type | text | YES | | NULL | |
| related_products | mediumtext | YES | | NULL | |
| product_url | text | YES | | NULL | |
| enable_flag | varchar(1) | YES | | NULL | |
| purchasable | varchar(1) | YES | | NULL | |
| price_msg | text | YES | | NULL | |
| in_stock_msg | text | YES | | NULL | |
| out_of_stock_msg | text | YES | | NULL | |
| variations | text | YES | | NULL | |
| attributes | text | YES | | NULL | |
| delete_flag | varchar(1) | YES | | NULL | |
| short_desc | text | YES | | NULL | |
| cross_ref | text | YES | | NULL | |
| model_no | varchar(40) | YES | | NULL | |
| pkg_qty | int(11) | YES | | 1 | |
| military_spec | varchar(40) | YES | | NULL | |
| grips | varchar(80) | YES | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
In this case, the problem was the database became confused on what I actually wanted to do. There is only one "products" table so the database should not have become confused - but it did. The solution is to add the database's name on to the command so there isn't any confusion. So the command should have been:
update store.products where enable_flag='Y' where prod_id=31750
By placing the "store" part on to the command, the database.table connection is then established and the field is found.
I suspect that your enable_flag might have other characters. You can figure this out using information_schema.columns:
select concat('"', c.column_name, '"')
from information_schema.columns c
where table_name = 'products' and column_name like '%enable%';
If you find extra characters, you will probably need backticks.

MySQL Query - Select all posts and count comments for each one

It's 3:30 AM in my country so I need to sleep but I can't without this:
I'm trying to get all posts (using Zend_Db) and count comments for each one.
Schema
blog_posts:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| content | text | NO | | NULL | |
| alias | varchar(100) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created_date | datetime | NO | | NULL | |
| modified_date | datetime | YES | | NULL | |
| thumbnail | varchar(255) | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
And here's blog_comments:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| post_id | int(11) | NO | | NULL | |
| comment | text | NO | | NULL | |
| created_date | datetime | NO | | NULL | |
| modified_date | datetime | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
Note: the blog_comments.post_id is linked with blog_posts.id.
I would like a resulting table like that:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| content | text | NO | | NULL | |
| alias | varchar(100) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created_date | datetime | NO | | NULL | |
| modified_date | datetime | YES | | NULL | |
| thumbnail | varchar(255) | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
| TEMPOROARY COLUMN IN OBJECT ($post->comment) |
+---------------+------------------+------+-----+---------+----------------+
| comments | | | | | |
+---------------+------------------+------+-----+---------+----------------+
Now, here's the query I have for now:
SELECT `p`.*, `c`.*
FROM `blog_posts` `p`
LEFT JOIN (
SELECT COUNT(*)
FROM `blog_comments` `c`
WHERE c.post_id = p.id
) ON `p`.`comments`;
But it give me an error:
Error Code: 1248. Every derived table must have its own alias
So if someone can help me, it would be very appreciated!
IMPORTANT NOTE
I'm using Zend_Db and Zend_Db_Select so I must be able to use the functions like joinLeft() or anything I need.
This is in my model for the select():
$select = $this->table->select();
if ($alias) {
$select->where('alias = ?', $alias);
return $this->table->fetchRow($select);
}
if ($withComments) {
// I WILL PLACE THE CODE HERE, EXEMPLE:
$select->joinLeft(...);
}
SELECT p.*, x.*
FROM blog_posts p
LEFT JOIN
(
SELECT post_id, COUNT(*) as cc
FROM blog_comments
GROUP BY post_id
) x
ON x.post_id = p.id;

Categories