我正在尝试创建一个数据库,其中可能有n多个类别及其子类别.
首先,我尝试创建这样的邻接模型数据库
+-------------+----------------------+--------+
| category_id | name | parent |
+-------------+----------------------+--------+
| 1 | Electronics | NULL |
| 2 | Mobile | 1 |
| 3 | Washing Machine | 1 |
| 4 | Samsung | 2 |
+-------------+----------------------+--------+
Run Code Online (Sandbox Code Playgroud)
但是,我在删除节点时遇到了问题,比如如何管理已删除节点的子节点等.
然后我试图实现Joe Celko的嵌套订单集

每个图中的表结构:
Figure 1:
+----+-------------+-----+-----+
| id | name | lft | rgt |
+----+-------------+-----+-----+
| 1 | Electronics | 1 | 2 |
+----+-------------+-----+-----+
Figure 2:
+----+-------------+-----+-----+
| id | name | …Run Code Online (Sandbox Code Playgroud) 我正在做一个相当大的SQL所以我道歉,不能提供我的表的更大的例子.
SELECT
customer_id,
agreement_id,
if( 'network' IN ( GROUP_CONCAT( DISTINCT services.service_code
SEPARATOR ',' ) ),
'Yes','No') as networkservice
FROM customers
INNER JOIN agreement USING(customer_id)
INNER JOIN services USING(agreement_id)
GROUP BY customer_id
Run Code Online (Sandbox Code Playgroud)
客户可以签订协议,协议可以提供很多服务.我想要弄清楚的是,"网络"是否是该协议中的服务之一.
由于GROUP_CONCAT返回逗号分隔列表,因此对我的情况来说感觉很完美.但我无法让它工作,我的想法已经不多了.
如果只有一个服务并且该服务是'network'则返回yes,但是如果有多个服务则返回No.
如果我使用(INT)service_id而不是没有区别,除非INT Im正在寻找列表中的第一个.但那仅适用于INT,如果'network'在列表中排在第一位,则返回No.
我试过了:
if( 'network' IN ( CAST(GROUP_CONCAT( DISTINCT services.service_code
SEPARATOR ' ' ) AS CHAR) ),
'Yes','No')
Run Code Online (Sandbox Code Playgroud)
和
if( 'network' IN ( concat('\'',
GROUP_CONCAT(DISTINCT services.service_code
SEPARATOR '\', \'' ),
'\'') ), 'Yes','No')
Run Code Online (Sandbox Code Playgroud)
如果我的解释听起来很混乱,我可以提供更多的例子.
谢谢.
有没有办法将查询应用于mysql数据库中的每个表?
就像是
SELECT count(*) FROM {ALL TABLES}
-- gives the number of count(*) in each Table
Run Code Online (Sandbox Code Playgroud)
和
DELETE FROM {ALL TABLES}
-- Like DELETE FROM TABLE applied on each Table
Run Code Online (Sandbox Code Playgroud) 我只是在VM上设置了debian 8.3并在本教程之后安装了xampp .一切正常,直到我尝试创建一个新表:
create table testtable
(
id int(10) not null auto_increment,
firstname varchar(255) collate utf8mb4_german2_ci not null,
lastname varchar(255) collate utf8mb4_german2_ci not null,
primary key (id),
unique key (lastname)
)engine = innodb default charset=utf8mb4, collate=utf8mb4_german2_ci
Run Code Online (Sandbox Code Playgroud)
我得到了错误:#1709 - Index column size too large. The maximum column size is 767 bytes.
然后我发现这来自于prefix limitation限制为767Byte Innodb并且我可以通过在my.cnf文件中设置innodb_large_prefix来解决这个问题.但我找不到文件,它不在下,/etc/而且没有 - 文件夹,我发现/etc/mysql/的唯一,但是,在我添加到文件并重新启动lampp之后.我仍然得到同样的错误.我做错了什么?my.cnf/opt/lampp/etc/innodb_large_prefix=1
编辑:SELECT version()返回5.6.14,所以innodb_large_prefix应该支持.
edit2:我知道我可以解决这个问题,只需将键的一部分设置为索引即可获得767Byte.但我想知道如何正确配置mysql.
我有两个独立的查询,它们具有相同的输出。现在我想了解哪一个更好?
查询1:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|-------------|-------|------|---------------|--------|---------|--------|------|----------------------------------------------------|
| 1 | SIMPLE | t1 | ALL | (null) | (null) | (null) | (null) | 9 | Using where |
| 1 | SIMPLE | t2 | ALL | (null) | (null) | (null) | (null) | 9 | Using where; Using join buffer (Block Nested Loop) |
Run Code Online (Sandbox Code Playgroud)
查询2:
| id | select_type | …Run Code Online (Sandbox Code Playgroud) 我想获取我周围的所有位置,但是该功能ST_Distance_Sphere不起作用。
我的查询:
select *, astext(location) as location from `locations`
where ST_Distance_Sphere(location, POINT(35.905069591297, 49.765869174153)) < 1000
Run Code Online (Sandbox Code Playgroud)
错误:
SQLSTATE[42000]: Syntax error or access violation:
1305 FUNCTION app.ST_Distance_Sphere does not exist (SQL:
select *, astext(location) as location from `locations`
where ST_Distance_Sphere(location, POINT(35.905069591297, 49.765869174153)) < 1000)
Run Code Online (Sandbox Code Playgroud) 我的本地服务器上有一个 MySQL 表。该表包括用户标记的地点的纬度和经度。我正在尝试获取距离所提供的纬度和经度 1 公里范围内标记其位置的所有 id。但我的结果却出乎我的意料。
表:map_locations
id user_id lat lng place_name
1 1 28.584688 77.31593 Sec 2, Noida
2 2 28.596026 77.314494 Sec 7, Noida
3 5 28.579876 77.356131 Sec 35, Noida
4 1 28.516831 77.487405 Surajpur, Greater Noida
5 1 28.631451 77.216667 Connaught Place, New Delhi
6 2 19.098003 72.83407 Juhu Airport, Mumbai
Run Code Online (Sandbox Code Playgroud)
这是 PHP 脚本
$lat = '28.596026';
$long = '77.314494';
$query = "SELECT id,
(6371 * acos( cos( radians($lat) ) * cos( radians('lat') ) *
cos( …Run Code Online (Sandbox Code Playgroud) CREATE TABLE `files` (
`did` int(10) unsigned NOT NULL DEFAULT '0',
`filename` varbinary(200) NOT NULL,
`ext` varbinary(5) DEFAULT NULL,
`fsize` double DEFAULT NULL,
`filetime` datetime DEFAULT NULL,
PRIMARY KEY (`did`,`filename`),
KEY `fe` (`filetime`,`ext`), -- This?
KEY `ef` (`ext`,`filetime`) -- or This?
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
Run Code Online (Sandbox Code Playgroud)
表中有一百万行.文件时间大多不同.数量有限ext.因此,filetime具有高基数并且ext具有低得多的基数.
该查询涉及ext和filetime:
WHERE ext = '...'
AND filetime BETWEEN ... AND ...
Run Code Online (Sandbox Code Playgroud)
这两个指标中的哪一个更好?为什么?
我有一个返回响应速度的查询.当timediff计算结果超过24小时时,它可以正常工作.我收到一个错误"java.sql.SQLException:非法小时值'31'为java.sal.Time类型的值"
"31"的数量随时间戳之间的小时差异而变化.
我不知道从哪里开始.我尝试timestampdiff()但尝试运行查询时出错.
任何意见或建议将不胜感激.
SELECT a.*
FROM
(SELECT
databunker.purchases.id AS 'pur_id',
databunker.purchases.business AS 'pur_business',
databunker.purchases.time AS 'pur_time',
databunker.customers.id AS 'customer_id',
databunker.customers.phone_number AS 'customer_phone_#',
databunker.customers.is_primary AS 'customer_is_primary',
map.object_salesforce_id,
databunker.five9_calls.campaign_name,
databunker.five9_calls.start_timestamp AS 'start_of_call',
TIMEDIFF(databunker.five9_calls.start_timestamp, databunker.purchases.time) AS 'speed_to_response'
FROM databunker.purchases
LEFT OUTER JOIN databunker.customers ON (databunker.purchases.customer_id = databunker.customers.id)
LEFT OUTER JOIN (SELECT
databunker.mappings.object_salesforce_id,
databunker.mappings.object_id AS 'map_customer_id'
FROM databunker.mappings
WHERE databunker.mappings.object_class = 'customer') AS map
ON (databunker.purchases.customer_id = map.map_customer_id)
LEFT OUTER JOIN databunker.five9_calls ON (map.object_salesforce_id =
databunker.five9_calls.salesforce_id)
WHERE databunker.purchases.business = 'uma'
AND databunker.purchases.outcome_type = …Run Code Online (Sandbox Code Playgroud) 我有 SQL 查询(参见示例)。但我找不到如何在查询生成器中编写它的方法。你有什么想法吗?这怎么可能?
WITH main AS (
SELECT id FROM table1
)
SELECT * FROM table2
WHERE
table2.id IN (SELECT * FROM main)
Run Code Online (Sandbox Code Playgroud)
我想获得如下格式:
$latestPosts = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)
->groupBy('user_id');
$users = DB::table('users')
->joinSub($latestPosts, 'latest_posts', function ($join) {
$join->on('users.id', '=', 'latest_posts.user_id');
})->get();
Run Code Online (Sandbox Code Playgroud)
但对于WITH
postgresql common-table-expression query-builder laravel eloquent