如何让bash选择一个空列?

时间:2014-04-15 20:39:13

标签: mysql sql bash

我在数据库中有两个表,它允许数据库中的每个电子邮件地址上传最多14个文件。如何使bash选择一个空列以允许上传文件?

提前致谢。

这两个表称为uploadid和uploadurl。

以下是两个表格:

表1 - 上传ID(上传名称)

+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| email_address  | varchar(256) | NO   | PRI | NULL    |       |
| 1_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 2_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 3_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 4_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 5_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 6_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 7_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 8_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 9_upload_id    | varchar(256) | YES  |     | NULL    |       |
| 10_upload_id   | varchar(256) | YES  |     | NULL    |       |
| 11_upload_id   | varchar(256) | YES  |     | NULL    |       |
| 12_upload_id   | varchar(256) | YES  |     | NULL    |       |
| 13_upload_id   | varchar(256) | YES  |     | NULL    |       |
| 14_upload_id   | varchar(256) | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

表2 - 上传网址(上传文件的链接)

+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| email_address  | varchar(256) | NO   | PRI | NULL    |       |
| 1_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 2_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 3_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 4_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 5_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 6_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 7_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 8_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 9_upload_url   | varchar(256) | YES  |     | NULL    |       |
| 10_upload_url  | varchar(256) | YES  |     | NULL    |       |
| 11_upload_url  | varchar(256) | YES  |     | NULL    |       |
| 12_upload_url  | varchar(256) | YES  |     | NULL    |       |
| 13_upload_url  | varchar(256) | YES  |     | NULL    |       |
| 14_upload_url  | varchar(256) | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:0)

你的数据库的设计非常糟糕但是没关系......你可以尝试类似的东西:

query = "SELECT"
for i in {1..14}; do
    query="$query COALESCE(${i}_upload_id, 'freeSpace') AS idFile${i}"
done
query="$query FROM table1 WHERE email = '$email';"

result=$(echo $query|mysql -u<user> -p<password> <dbname>)
[[ $result =~ freeSpace ]] && echo "$email can upload files" || echo "$email can't upload files"

这是Bash可以做些什么的一个例子,但我不确定这是你想要做的最好的技术。

否则,这是一个更智能的数据库设计示例:

User(id, email) 
    => PK constraint on column "id" (auto_increment)
    => UNIQUE constraint on column "email"

File(id_file, uri_file, id_user)
    => PK constraint  on column "id_file"
    => FK constraint on column "id_user" (references User.id)

要知道用户是否能够上传新文件,您可以运行此查询:

SELECT COUNT(id_file) AS nbFiles FROM File WHERE id_user = (SELECT id FROM User WHERE email = '$email');

如果nbFiles&gt; = 14,则用户将无法上传新文件;)