Mysql Joint joints primary key - php

I would like to know how can I do joints in this case :
I have a table named : Table_ref that contains the name of all the table in the database, with the structure :
-----------------------------------------------
| Field | Type | Null | Key |
-----------------------------------------------
| tbl_name | varchar(45) | NO | PRI |
| tbl_type | Tinyint(3) | NO | MUL |
-----------------------------------------------
and forteen other table with names like : A1,B1, A2,B2 ... with the same structure :
-----------------------------------------------
| Field | Type | Null | Key |
-----------------------------------------------
| id | int(10) | NO | PRI |
| itime | int(10) | YES | MUL |
| dtime | int(10) | YES | MUL |
| src | varchar(40) | YES | |
| dstname | varchar(255)| YES | |
-----------------------------------------------
The question is how can I do joints in order to extract information where src='192.168.1.2' from all the tables.

This is the combined structure for all your tables
-----------------------------------------------
| Field | Type | Null | Key |
-----------------------------------------------
| id | int(10) | NO | PRI |
| itime | int(10) | YES | MUL |
| dtime | int(10) | YES | MUL |
| src | varchar(40) | YES | |
| dstname | varchar(255)| YES | |
| type | Tinyint(3) | NO | MUL |
-----------------------------------------------
And now you can extract information with a simplist query
SELECT
*
FROM mytable
WHERE src = '192.168.1.2'
It will fetch records from every type. If you need information form
specific type just add another where condition
SELECT
*
FROM mytable
WHERE src = '192.168.1.2'
AND type = 1

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

Stop duplicate rows in dynamic drop down

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;

Doing a proper Join with SQL

Suppose I have two tables, one with list of spells and another with a grimory, the list of spells that a user has selected for learn or already learned.
mysql> SHOW COLUMNS FROM Grimory;
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| personage_id | int(11) | YES | MUL | NULL | |
| spell_id | int(11) | YES | MUL | NULL | |
| isLearned | tinyint(1) | NO | | NULL | |
| isSelected | tinyint(1) | NO | | NULL | |
+--------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM Spell;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| description | longtext | NO | | NULL | |
| chatDescription | longtext | NO | | NULL | |
| level | int(11) | NO | | NULL | |
| isActive | tinyint(1) | NO | | NULL | |
| category_id | int(11) | YES | MUL | NULL | |
| createdAt | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
The problem.
I want to display a list with ALL spells by category_id, BUT for every row I want to show if that spell is learned or selected by current user (personage_id).
Can you help me to write a proper query?
How this works:
With php I generate: List of spells | checkbox isSelected | checkBox isStudied
When I click on isSelected checkbox, a record in grimory will be added with current user and spell.
Updated
SELECT a.*, IFNULL(b.isLearned,0) as isLearned,
IFNULL(b.isSelected,0) as isSelected
FROM Spell a
LEFT JOIN Grimory b(ON b.spell_id =a.id
AND b.personage_id =:current_user_id)
WHERE a.category_id = :current_category_id
SELECT * FROM Spell
INNER JOIN Grimory
ON Spell.id = Grimory.spell_id
WHERE (Grimory.isLearned = 1 OR Grimory.isSelected = 1)
AND Spell.category_id = 'YOUR CAT ID IS HERE'

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;

Force Specific Record to Top When Performing GROUP BY

I have the following MySQL query and tables from which I am querying:
SELECT
`Song`.`id`,
`Song`.`file_name`,
`User`.`first_name`,
`Vote`.`value`,
Sum(`Vote`.`value`) AS score
FROM `songs` AS `Song`
LEFT JOIN votes AS `Vote` ON (`Song`.`id`=`Vote`.`song_id`)
LEFT JOIN `users` AS `User` ON (`Song`.`user_id` = `User`.`id`)
GROUP BY `Vote`.`song_id`
LIMIT 20;
mysql> describe songs;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| file_name | varchar(255) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> describe users;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| is_admin | tinyint(1) | NO | | 0 | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
mysql> describe votes;
+----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| value | int(11) | NO | | NULL | |
| song_id | int(11) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+----------+----------+------+-----+---------+----------------+
This query functions just like I want except for one thing. The value returned in the field Vote.value is not from a row that is associated with the user who is logged into the application. I need the score value to be a sum of all the values no matter which user it is associated with, but the Vote.value field should belong to the logged in user (each user only gets one vote record per song).
My first thought is to somehow sort the table so that when the group by happens the vote record for the logged in user is at the top but I have no idea how to do a sort that forces an arbitrary value to the top. Any ideas would be very helpful.
and a third join
LEFT JOIN votes AS `VotePerUser` ON (`Song`.`id`=`Vote`.`song_id`
AND `Song`.`user_id`=`votes`.`user_id`)
and replace the Vote.value with VotePerUser.Value

Categories